forked from lucab/ntop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.c
1702 lines (1387 loc) · 55.2 KB
/
admin.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
/*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* http://www.ntop.org/
*
* 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,
* 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.
*/
#include "ntop.h"
#include "globals-report.h"
/* Forward */
static void sendMenuFooter(int itm1Idx, int itm2Idx);
static void encodeWebFormURL(char *in, char *buf, int buflen);
static void decodeWebFormURL(char *buf);
static int processNtopConfigData (char *buf, int savePref);
/* ****************************** */
void showUsers(void) {
u_int numUsers=0;
char buf[LEN_GENERAL_WORK_BUFFER];
datum key_data, return_data;
printHTMLheader("Registered ntop Users", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
return_data = gdbm_firstkey(myGlobals.pwFile);
while (return_data.dptr != NULL) {
/* traceEvent(CONST_TRACE_INFO, "1) -> %s", return_data.dptr); */
key_data = return_data;
if(key_data.dptr[0] == '1') /* 1 = user */{
if(numUsers == 0) {
sendString("<CENTER>\n"
""TABLE_ON"<TABLE BORDER=1 "TABLE_DEFAULTS">\n");
sendString("<TR "DARK_BG"><TH "TH_BG">Users</TH><TH "TH_BG">Actions</TH></TR>\n");
}
if(strcmp(key_data.dptr, "1admin") == 0) {
safe_snprintf(__FILE__, __LINE__, buf, LEN_GENERAL_WORK_BUFFER,
"<tr><th "TH_BG" align=\"left\"><img src=\"/user.gif\">"
" %s</th><td "TD_BG"><a href=\"/%s?%s\">"
"<img class=tooltip alt=\"Modify User\" src=\"/"CONST_EDIT_IMG"\" "
"border=\"0\" align=\"absmiddle\"></a>"
" </td></tr></th></tr>\n",
&key_data.dptr[1], CONST_MODIFY_USERS, key_data.dptr);
} else{
char ebuf[256];
encodeWebFormURL(key_data.dptr, ebuf, sizeof(ebuf));
safe_snprintf(__FILE__, __LINE__, buf, LEN_GENERAL_WORK_BUFFER,
"<tr><th "TH_BG" align=\"left\"><img src=\"/user.gif\">"
" %s</tg><td "TD_BG"><a href=\"/%s?%s\">"
"<img class=tooltip alt=\"Modify User\" src=\"/"CONST_EDIT_IMG"\" border=\"0\" "
"align=\"absmiddle\"></a>"
" <A HREF=/%s?%s>"
"<img class=tooltip alt=\"Delete User\" src=\"/deleteUser.gif\" border=\"0\" "
"align=\"absmiddle\">"
"</a></td></tr></th></tr>\n",
&key_data.dptr[1], CONST_MODIFY_USERS, ebuf, CONST_DELETE_USER, ebuf);
}
sendString(buf);
numUsers++;
}
return_data = gdbm_nextkey(myGlobals.pwFile, key_data);
free(key_data.dptr);
}
if(numUsers > 0) {
sendString("</TABLE>"TABLE_OFF"\n<P>\n");
sendString("</CENTER>\n");
}
sendMenuFooter(1, 2);
}
/* ****************************** */
void clearUserUrlList(void) {
int i;
/*
* We just changed the database.
* Delete the in-memory copy so next time we reference it,
* it will be reloaded with the new values
*/
traceEvent(CONST_TRACE_NOISY, "SECURITY: Loading items table");
if(myGlobals.securityItemsMutex.isInitialized == 1)
accessMutex(&myGlobals.securityItemsMutex, "clear");
for (i=0; i<myGlobals.securityItemsLoaded; i++) {
free(myGlobals.securityItems[i]);
}
myGlobals.securityItemsLoaded = 0;
if(myGlobals.securityItemsMutex.isInitialized == 1)
releaseMutex(&myGlobals.securityItemsMutex);
}
/* ****************************** */
void addUser(char* user) {
char tmpStr[128];
printHTMLheader("Manage ntop Users", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
if((user != NULL) && ((strlen(user) < 2) || (user[0] != '1'))) {
printFlagedWarning("<I>The specified username is invalid.</I>");
} else {
sendString("<CENTER>\n");
sendString("<script Language=\"JavaScript\">\nfunction CheckForm(theForm) "
"{\nif(theForm.pw.value != theForm.pw1.value) {\n alert(\"Passwords do not match. "
"Please try again.\");\n theForm.pw1.focus();\n return(false);\n }\n return(true);"
"\n}\n</script>\n");
sendString("<FORM METHOD=POST ACTION=/doAddUser onsubmit=\"return CheckForm(this)\">\n");
sendString("<TABLE BORDER=0 "TABLE_DEFAULTS">\n");
sendString("<TR>\n<TH ALIGN=right>User: </TH><TD ALIGN=left>");
if(user != NULL) {
decodeWebFormURL(user);
safe_snprintf(__FILE__, __LINE__, tmpStr, sizeof(tmpStr),
"<INPUT TYPE=hidden NAME=user SIZE=20 VALUE=\"%s\"><B>%s</B>\n",
&user[1], &user[1]);
sendString(tmpStr);
} else
sendString("<INPUT TYPE=text NAME=user SIZE=20>\n");
sendString("</TD>\n</TR>\n");
sendString("<TR><TH ALIGN=right>Password: </TH>"
"<TD ALIGN=left><INPUT TYPE=password NAME=pw SIZE=20></TD></TR>\n");
sendString("<TR><TH ALIGN=right>Verify Password: </TH>"
"<TD ALIGN=left><INPUT TYPE=password NAME=pw1 SIZE=20></TD></TR>\n");
/*********** Communities **********/
{
int i, numUsers=0, len = strlen(COMMUNITY_PREFIX);
datum key_data, return_data;
char *aubuf=NULL, *userCommunities[20], communities[128], key[256];
sendString("<TR><TH ALIGN=right VALIGN=top>Authorised Communities: </TH>"
"<TD ALIGN=left>\n<SELECT NAME=communities MULTIPLE>\n");
memset(userCommunities, 0, sizeof(userCommunities));
if(user != NULL) {
snprintf(key, sizeof(key), "%s%s", COMMUNITY_PREFIX, &user[1]);
if(fetchPwValue(key, communities, sizeof(communities)) == 0) {
char *strtokState, *item;
item = strtok_r(communities, "&", &strtokState);
for(i=0; (item != NULL) && (i < sizeof(userCommunities)-1); i++) {
userCommunities[i] = item;
item = strtok_r(NULL, "&", &strtokState);
}
if(item != NULL)
traceEvent(CONST_TRACE_ERROR, "Too many communities for user='%s'", &user[1]);
userCommunities[i] = NULL;
}
}
return_data = gdbm_firstkey(myGlobals.prefsFile);
while(return_data.dptr != NULL) {
key_data = return_data;
if(!strncmp(key_data.dptr, COMMUNITY_PREFIX, len)) {
int found = 0;
char *communityName = &key_data.dptr[len];
for(i=0; userCommunities[i] != NULL; i++)
if(strcmp(userCommunities[i], communityName) == 0) {
found = 1;
}
/* Make sure that at least a user is selected */
if((numUsers == 0) && (userCommunities[0] == NULL)) found = 1;
safe_snprintf(__FILE__, __LINE__, tmpStr, sizeof(tmpStr),
"<option value=%s %s>%s</option>\n",
communityName,
found ? "SELECTED" : "",
communityName);
sendString(tmpStr);
numUsers++;
}
return_data = gdbm_nextkey(myGlobals.prefsFile, key_data);
free(key_data.dptr);
}
if(aubuf != NULL) free(aubuf); /* (**) */
sendString("</SELECT>\n</TD></TR>\n");
}
sendString("</TABLE>"TABLE_OFF"\n");
safe_snprintf(__FILE__, __LINE__, tmpStr, sizeof(tmpStr),
"<INPUT TYPE=submit VALUE=\"%s\"> <INPUT TYPE=reset VALUE=Reset>\n",
(user != NULL) ? "Modify User" : "Add User");
sendString(tmpStr);
sendString("</FORM>\n");
sendString("</CENTER>\n");
}
sendMenuFooter(0, 2);
}
/* ****************************** */
void deleteUser(char* user) {
if(user == NULL) {
returnHTTPredirect(CONST_SHOW_USERS_HTML);
return;
} else if((strlen(user) < 2) || (user[0] != '1')) {
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("Delete ntop User", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
printFlagedWarning("<I>The specified username is invalid.</I>");
} else {
int rc;
datum key_data;
decodeWebFormURL(user);
key_data.dptr = user;
key_data.dsize = strlen(user)+1;
/* Delete a URL - clear the list */
clearUserUrlList();
rc = gdbm_delete(myGlobals.pwFile, key_data);
if(rc != 0) {
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("Delete ntop User", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
printFlagedWarning("<B>ERROR:</B> <I>unable to delete specified user.</I>");
} else {
returnHTTPredirect(CONST_SHOW_USERS_HTML);
return;
}
}
sendMenuFooter(1, 2);
printHTMLtrailer();
}
/* ****************************** */
#define MAX_NUM_COMMUNITIES 32
void doAddUser(int len) {
char *err=NULL, key_str[64], value_str[256];
int j;
if(len <= 0) {
err = "ERROR: both user and password must be non empty fields.";
} else {
char postData[256], *key, *user=NULL, *pw=NULL, *communities[MAX_NUM_COMMUNITIES];
int i, idx, badChar=0, num_communities = 0;
if((idx = readHTTPpostData(len, postData, sizeof(postData))) < 0)
return; /* FIXME (DL): an HTTP error code should be sent here */
for(i=0,key=postData; i<idx; i++) {
if(postData[i] == '&') {
postData[i] = '\0';
key = &postData[i+1];
} else if((key != NULL) && (postData[i] == '=')) {
postData[i] = '\0';
if(strcmp(key, "user") == 0)
user = &postData[i+1];
else if(strcmp(key, "pw") == 0)
pw = &postData[i+1];
else if(strcmp(key, "communities") == 0) {
if(num_communities+1 < MAX_NUM_COMMUNITIES) {
communities[num_communities] = strdup(&postData[i+1]);
for(j=0; j<strlen(communities[num_communities]); j++)
if(communities[num_communities][j] == '&') {
communities[num_communities][j] = '\0';
break;
}
num_communities++;
}
}
key = NULL;
}
}
if(user != NULL) {
decodeWebFormURL(user);
for(i=0; i<strlen(user); i++) {
if(!(isalpha(user[i]) || isdigit(user[i]))) {
badChar = 1;
break;
}
}
}
if(pw != NULL)
decodeWebFormURL(pw);
#if 0
printf("User='%s' - Pw='%s'\n", user?user:"(not given)", pw?pw:"(not given)");
fflush(stdout);
#endif
if((user == NULL ) || (user[0] == '\0') || (pw == NULL) || (pw[0] == '\0')) {
err = "ERROR: both user and password must be non empty fields.";
} else if(badChar) {
err = "ERROR: the specified user name contains invalid characters.";
} else {
char tmpBuf[64];
#ifndef WIN32
/* 14 is ok for traditional crypt, but the enhanced crypt() in FreeBSD
* (and others?) can be much larger. Just us a big buffer for ALL OSes
* in case others change too...
*/
char cpw[LEN_MEDIUM_WORK_BUFFER];
#endif
datum data_data, key_data;
safe_snprintf(__FILE__, __LINE__, tmpBuf, sizeof(tmpBuf), "1%s", user);
key_data.dptr = tmpBuf;
key_data.dsize = strlen(tmpBuf)+1;
#ifdef WIN32
data_data.dptr = pw;
#else
strncpy(cpw, (char*)crypt(pw, (const char*)CONST_CRYPT_SALT), sizeof(cpw));
cpw[sizeof(cpw)-1] = '\0';
data_data.dptr = cpw;
#endif
data_data.dsize = strlen(data_data.dptr)+1;
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "User='%s' - Pw='%s [%s]'", user, pw, data_data.dptr);
#endif
snprintf(key_str, sizeof(key_str), "%s%s", COMMUNITY_PREFIX, user);
value_str[0] = '\0';
if(num_communities > 0) {
strcat(value_str, communities[0]);
for(j=1; j<num_communities; j++) {
strcat(value_str, "&");
strcat(value_str, communities[j]);
}
//traceEvent(CONST_TRACE_INFO, "========-----> [%s][%s]", key_str, value_str);
storePwValue(key_str, value_str);
}
if(gdbm_store(myGlobals.pwFile, key_data, data_data, GDBM_REPLACE) != 0)
err = "FATAL ERROR: unable to add the new user.";
/* Added user, clear the list */
clearUserUrlList();
}
}
if(err != NULL) {
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("ntop: Add/Modify User", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
printFlagedWarning(err);
sendMenuFooter(1, 2);
printHTMLtrailer();
} else {
returnHTTPredirect(CONST_SHOW_USERS_HTML);
}
}
/* ***********************************
*********************************** */
void showURLs(void) {
u_int numUsers=0;
char buf[LEN_GENERAL_WORK_BUFFER], ebuf[256];
datum key_data, return_data;
printHTMLheader("Restricted ntop URLs", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
return_data = gdbm_firstkey(myGlobals.pwFile);
while (return_data.dptr != NULL) {
/* traceEvent(CONST_TRACE_INFO, "1) -> %s", return_data.dptr); */
key_data = return_data;
if(key_data.dptr[0] == '2') { /* 2 = URL */
if(numUsers == 0) {
sendString("<CENTER>\n"
""TABLE_ON"<TABLE BORDER=1 "TABLE_DEFAULTS">\n");
sendString("<TR "DARK_BG"><TH "TH_BG">URLs</TH><TH "TH_BG">Actions</TH></TR>\n");
}
encodeWebFormURL(key_data.dptr, ebuf, sizeof(ebuf));
safe_snprintf(__FILE__, __LINE__, buf, LEN_GENERAL_WORK_BUFFER, "<TR><TH "TH_BG" ALIGN=LEFT><IMG SRC=/user.gif>"
" '%s*'</TH><TD "TD_BG"><A HREF=/%s?%s>"
"<IMG CLASS=TOOLTIP ALT=\"Modify URL\" SRC=/"CONST_EDIT_IMG" BORDER=0 align=absmiddle></A>"
" <A HREF=/%s?%s><IMG CLASS=TOOLTIP ALT=\"Delete URL\" SRC=/deleteUser.gif BORDER=0 align=absmiddle>"
"</A></TD></TR></TH></TR>\n", &key_data.dptr[1], CONST_MODIFY_URL, ebuf, CONST_DELETE_URL, ebuf);
sendString(buf);
numUsers++;
}
return_data = gdbm_nextkey(myGlobals.pwFile, key_data);
free(key_data.dptr);
}
if(numUsers > 0) {
sendString("</TABLE>"TABLE_OFF"\n<P>\n");
sendString("</CENTER>\n");
}
sendMenuFooter(3, 0);
}
/* ****************************** */
void addURL(char* url) {
int i, numUsers=0;
datum key_data, return_data;
char *aubuf=NULL, *authorisedUser[20];
char tmpStr[128];
printHTMLheader("Manage ntop URLs", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
if((url != NULL) && ((strlen(url) < 1) || (url[0] != '2'))) {
printFlagedWarning("<I>The specified URL is invalid.</I>");
} else {
sendString("<CENTER>\n");
sendString("<FORM METHOD=POST ACTION=/doAddURL>\n");
sendString("<TABLE BORDER=0 "TABLE_DEFAULTS">\n");
if(url != NULL)
sendString("<TR>\n<TH ALIGN=right VALIGN=top><B>URL</B>: </TH>");
else
sendString("<TR>\n<TH ALIGN=right VALIGN=middle><B>URL</B>: </TH>");
sendString("<TD ALIGN=left><TT>http://<"
"<I>ntop host</I>>:<<I>ntop port</I>>/</TT>");
if(url != NULL) {
decodeWebFormURL(url);
safe_snprintf(__FILE__, __LINE__, tmpStr, sizeof(tmpStr),
"<INPUT TYPE=hidden NAME=url SIZE=20 VALUE=\"%s\">"
"<B>%s</B> <B>*</B> [Initial URL string]",
&url[1], &url[1]);
sendString(tmpStr);
} else {
sendString("<INPUT TYPE=text NAME=url SIZE=20> *");
}
sendString("</TD>\n</TR>\n");
/*********** Users **********/
sendString("<TR>\n<TH ALIGN=right VALIGN=top>Authorised Users: </TH>"
"<TD ALIGN=left><SELECT NAME=users MULTIPLE>\n");
authorisedUser[0] = NULL;
if(url != NULL) {
key_data.dptr = url;
key_data.dsize = strlen(url)+1;
return_data = gdbm_fetch(myGlobals.pwFile, key_data);
if(return_data.dptr != NULL) {
char *strtokState, *item;
aubuf = return_data.dptr; /* freed later (**) */
item = strtok_r(aubuf, "&", &strtokState);
for(i=0; (item != NULL) && (i < sizeof(authorisedUser)-1); i++) {
authorisedUser[i] = &item[sizeof("users=")-1];
item = strtok_r(NULL, "&", &strtokState);
}
if(item != NULL) {
traceEvent(CONST_TRACE_ERROR, "Too many users for URL='%s'", url);
}
authorisedUser[i] = NULL;
}
}
return_data = gdbm_firstkey(myGlobals.pwFile);
while (return_data.dptr != NULL) {
key_data = return_data;
if(key_data.dptr[0] == '1') { /* 1 = user */
int found = 0;
for(i=0; authorisedUser[i] != NULL; i++) {
if(strcmp(authorisedUser[i], key_data.dptr) == 0)
found = 1;
}
/* Make sure that at least a user is selected */
if((numUsers == 0) && (authorisedUser[0] == NULL)) found = 1;
safe_snprintf(__FILE__, __LINE__, tmpStr, sizeof(tmpStr),
"<option value=%s %s>%s</option>",
key_data.dptr, found ? "SELECTED" : "", &key_data.dptr[1]);
sendString(tmpStr);
numUsers++;
}
return_data = gdbm_nextkey(myGlobals.pwFile, key_data);
free(key_data.dptr);
}
if(aubuf != NULL) free(aubuf); /* (**) */
sendString("</SELECT>\n</TD></TR>\n");
if(url == NULL)
sendString( "<tr><td colspan=2 align=left><U>NOTE</U>: if you leave the URL field empty then the "
"access is restricted to <I>all</I> ntop pages, otherwise,<br>this "
"entry matches all the pages begining with the specified string.</B>\n"
"</td>\n</tr>\n");
sendString("</TABLE>"TABLE_OFF"\n");
safe_snprintf(__FILE__, __LINE__, tmpStr, sizeof(tmpStr),
"<INPUT TYPE=submit VALUE=\"%s\"> <INPUT TYPE=reset VALUE=Reset>\n",
(url != NULL) ? "Modify URL" : "Add URL");
sendString(tmpStr);
sendString("</FORM>\n");
sendString("</CENTER>\n");
}
sendMenuFooter(0, 2);
}
/* ****************************** */
void deleteURL(char* url) {
if(url == NULL) {
returnHTTPredirect(CONST_SHOW_URLS_HTML);
return;
} else if((strlen(url) < 1) || (url[0] != '2')) {
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("Delete ntop URL", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
printFlagedWarning("<I>The specified URL is invalid.</I>");
} else {
int rc;
datum key_data;
decodeWebFormURL(url);
key_data.dptr = url;
key_data.dsize = strlen(url)+1;
/* Delete URL, clear the list */
clearUserUrlList();
rc = gdbm_delete(myGlobals.pwFile, key_data);
if(rc != 0) {
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("Delete ntop URL", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
printFlagedWarning("<B>ERROR:</B> <I>unable to delete specified URL.</I>");
} else {
returnHTTPredirect(CONST_SHOW_URLS_HTML);
return;
}
}
sendMenuFooter(3, 0);
printHTMLtrailer();
}
/* ****************************** */
void doAddURL(int len) {
char *err=NULL;
char postData[256], *key, *url=NULL, *users=NULL, authorizedUsers[256];
int i, idx, alen=0, badChar=0;
/*
Authorization fix
courtesy of David Brown <[email protected]>
*/
if((idx = readHTTPpostData(len, postData, sizeof(postData))) < 0)
return; /* FIXME (DL): an HTTP error code should be sent here */
memset(authorizedUsers, 0, sizeof(authorizedUsers));
for(i=0,key=postData; i<=idx; i++) {
if((i==idx) || (postData[i] == '&')) {
if(users != NULL) {
decodeWebFormURL(users);
safe_snprintf(__FILE__, __LINE__, &authorizedUsers[alen], sizeof(authorizedUsers)-alen,
"%susers=%s", (alen>0) ? "&" : "", users);
alen = strlen(authorizedUsers);
users = NULL;
}
if(i==idx) break;
postData[i] = '\0';
key = &postData[i+1];
} else if((key != NULL) && (postData[i] == '=')) {
postData[i] = '\0';
if(strcmp(key, "url") == 0) {
url = &postData[i+1];
} else if(strcmp(key, "users") == 0) {
users = &postData[i+1];
}
key = NULL;
}
}
if(url != NULL) {
decodeWebFormURL(url);
for(i=0; i<strlen(url); i++) {
if(!(isalpha(url[i]) || isdigit(url[i]) || (strchr("/-_?.", url[i]) != NULL))) {
badChar = 1;
break;
}
}
}
#if 0
printf("URL: '%s' - users: '%s'\n", url ? url : "(not given)",
strlen(authorizedUsers) > 0 ? authorizedUsers : "(not given)");
fflush(stdout);
#endif
if(authorizedUsers[0] == '\0') {
err = "ERROR: user must be a non empty field.";
} else if(badChar) {
err = "ERROR: the specified URL contains invalid characters.";
} else {
char tmpBuf[64];
datum data_data, key_data;
safe_snprintf(__FILE__, __LINE__, tmpBuf, sizeof(tmpBuf), "2%s", url);
key_data.dptr = tmpBuf;
key_data.dsize = strlen(tmpBuf)+1;
data_data.dptr = authorizedUsers;
data_data.dsize = strlen(authorizedUsers)+1;
if(gdbm_store(myGlobals.pwFile, key_data, data_data, GDBM_REPLACE) != 0)
err = "FATAL ERROR: unable to add the new URL.";
/* Added url, clear the list */
clearUserUrlList();
}
if(err != NULL) {
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("ntop URL add", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR><P>\n");
printFlagedWarning(err);
sendMenuFooter(3, 0);
printHTMLtrailer();
} else {
returnHTTPredirect(CONST_SHOW_URLS_HTML);
}
}
/* ********************************* */
void setPcapFilter(char* filters, int device_id) {
if(filters != NULL) {
char *filters_copy;
char buf[2048];
if(filters[0] == '@') {
/* This is a file */
filters_copy = read_file(filters, buf, sizeof(buf));
if(filters_copy)
filters_copy = strdup(buf);
} else
filters_copy = strdup(filters);
if(filters_copy) {
char find_token[32], *the_filter;
/*
Format is
eth0="filter for eth0",eth1="filter for eth1"....
or
"filter for all interfaces"
*/
if(strstr(filters_copy, "=") == NULL) {
/* This is a filter for all interfaces */
the_filter = filters_copy;
} else {
safe_snprintf(__FILE__, __LINE__,
find_token, sizeof(find_token),
"%s=", myGlobals.device[device_id].name);
the_filter = strstr(filters_copy, find_token);
if(the_filter != NULL) {
/* We have an interface specific filter */
char *quote;
the_filter += strlen(find_token);
quote = strchr(the_filter, ',');
if(quote != NULL) quote[0] = '\0';
} else {
traceEvent(CONST_TRACE_INFO,
"No filter specified for interface %s",
myGlobals.device[device_id].name);
return;
}
}
if(the_filter != NULL) {
struct bpf_program fcode;
traceEvent(CONST_TRACE_INFO,
"Using filter '%s' for interface %s",
the_filter, myGlobals.device[device_id].name);
if(myGlobals.device[device_id].pcapPtr
&& (!myGlobals.device[device_id].virtualDevice)) {
if((pcap_compile(myGlobals.device[device_id].pcapPtr, &fcode,
the_filter, 1,
myGlobals.device[device_id].netmask.s_addr) < 0)
|| (pcap_setfilter(myGlobals.device[device_id].pcapPtr, &fcode) < 0)) {
traceEvent(CONST_TRACE_ERROR,
"Wrong filter '%s' (%s) on interface %s",
the_filter,
pcap_geterr(myGlobals.device[device_id].pcapPtr),
myGlobals.device[device_id].name);
} else {
pcap_freecode(&fcode);
if(*the_filter!='\0') {
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "Set filter \"%s\" on interface %s",
the_filter, myGlobals.device[device_id].name);
} else {
traceEvent(CONST_TRACE_ALWAYSDISPLAY,
"Set no kernel (libpcap) filtering on interface %s",
myGlobals.device[device_id].name);
}
}
}
}
free(filters_copy);
}
}
}
/* ****************************** */
/* Courtesy of Michael Weidel <[email protected]> */
int doChangeFilter(int len) {
int i,idx,badChar=0;
char *currentFilterExpressionSav;
char buf[LEN_GENERAL_WORK_BUFFER],postData[256],*key,*err=NULL;
currentFilterExpressionSav = strdup(myGlobals.runningPref.currentFilterExpression); /* Backup */
if((idx = readHTTPpostData(len, postData, sizeof(postData))) < 0)
return 1;
for(i=0,key=postData; i<=idx; i++) {
if(postData[i] == '&') {
postData[i] = '\0';
key = &postData[i+1];
} else if((key != NULL) && (postData[i] == '=')) {
postData[i] = '\0';
if(strcmp(key, "filter") == 0) {
myGlobals.runningPref.currentFilterExpression = strdup(&postData[i+1]);
}
key = NULL;
}
}
if(key == NULL) {
decodeWebFormURL(myGlobals.runningPref.currentFilterExpression);
for(i=0; i<strlen(myGlobals.runningPref.currentFilterExpression); i++) {
if(!(isalpha(myGlobals.runningPref.currentFilterExpression[i]) ||
isdigit(myGlobals.runningPref.currentFilterExpression[i]) ||
(strchr("/-+*_.!&|><=\\\":[]() ", myGlobals.runningPref.currentFilterExpression[i]) != NULL))) {
badChar = 1; /* Perhaps we don't have to use this check? */
break;
}
}
} else
err = "ERROR: The HTTP Post Data was invalid.";
if(badChar)
err = "ERROR: the specified filter expression contains invalid characters.";
if(err == NULL) {
traceEvent(CONST_TRACE_INFO, "Changing the kernel (libpcap) filter...");
for(i=0; i<myGlobals.numDevices; i++)
setPcapFilter(myGlobals.runningPref.currentFilterExpression, i);
}
sendHTTPHeader(FLAG_HTTP_TYPE_HTML, 0, 1);
printHTMLheader("changing kernel (libpcap) filter expression", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<P><HR></P>\n<P><CENTER>");
sendString("<FONT FACE=\"Helvetica, Arial, Sans Serif\">\n");
if(err == NULL) {
if(*myGlobals.runningPref.currentFilterExpression != '\0'){
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
"<B>Filter changed to <I>%s</I>.</B></FONT>\n",
myGlobals.runningPref.currentFilterExpression);
sendString(buf);
} else {
sendString("<B>Kernel (libpcap) filtering disabled.</B></FONT>\n");
}
sendString("</CENTER></P>\n");
printHTMLtrailer();
if(currentFilterExpressionSav != NULL)
free(currentFilterExpressionSav);
return 0;
}
if(myGlobals.runningPref.currentFilterExpression != NULL)
free(myGlobals.runningPref.currentFilterExpression);
/* restore old filter expression */
myGlobals.runningPref.currentFilterExpression = currentFilterExpressionSav;
for(i=0; i<myGlobals.numDevices; i++)
setPcapFilter(myGlobals.runningPref.currentFilterExpression, i);
printFlagedWarning(err);
printHTMLtrailer();
return 2;
}
/* ******************************* */
/* Courtesy of Michael Weidel <[email protected]> */
void changeFilter(void) {
char buf[LEN_GENERAL_WORK_BUFFER];
printHTMLheader("Change kernel (libpcap) filter expression", NULL, BITFLAG_HTML_NO_REFRESH);
sendString("<BR><HR><P><center>\n");
sendString("<TABLE BORDER=1 "TABLE_DEFAULTS">\n<TR>\n");
sendString("<TH "TH_BG" ALIGN=center>Old Filter Expression: </TH><TD ALIGN=left>");
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), "<B>%s",
myGlobals.runningPref.currentFilterExpression);
sendString(buf);
if(*myGlobals.runningPref.currentFilterExpression == '\0') sendString("<No filter defined>");
sendString("</B><BR>\n</TD>\n</TR>\n");
sendString("<FORM METHOD=POST ACTION=/doChangeFilter>\n");
sendString("<TR>\n<TH "TH_BG" ALIGN=center>New Filter Expression: </TH>");
sendString("<TD ALIGN=left><INPUT TYPE=text NAME=filter SIZE=40>\n");
sendString("</TD>\n</TR>\n");
sendString("<TR><TD ALIGN=CENTER COLSPAN=2><INPUT TYPE=submit VALUE=\"Change Filter\"> ");
sendString("<INPUT TYPE=reset VALUE=Reset></TD></TR></FORM></TABLE>"TABLE_OFF"\n");
sendString("</B><P></CENTER>\n<FONT FACE=\"Helvetica, Arial, Sans Serif\">\n");
sendString("You can use all filter expressions libpcap can handle, \n");
sendString("like the ones you pass to tcpdump.<BR>\n");
sendString("If \"new filter expression\" is left empty, no filtering is performed.<BR>\n");
sendString("If you want the statistics to be reset, you have to do that manually ");
sendString("with <A HREF=\"" CONST_RESET_STATS_HTML "\">Reset Stats</A>.<BR>\n");
sendString("<B>Be careful</B>: That can take quite a long time!");
sendString("<BR><B></FONT>\n");
}
/* ****************************** */
struct _menuData {
char *text, *anchor;
};
static struct _menuData menuItem[] = {
{ "Show Users", CONST_SHOW_USERS_HTML },
{ "Add User", CONST_ADD_USERS_HTML },
{ "Show URLs", CONST_SHOW_URLS_HTML },
{ "Add URL", CONST_ADD_URLS_HTML }
};
/* ****************************** */
static void sendMenuFooter(int itm1Idx, int itm2Idx) {
char buf[128];
sendString("<CENTER>\n");
sendString("<FONT FACE=\"Helvetica, Arial, Sans Serif\">\n");
safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf),
"[ <A HREF=/%s>%s</A> ] [ <A HREF=/%s>%s</A> ]\n",
menuItem[itm1Idx].anchor, menuItem[itm1Idx].text,
menuItem[itm2Idx].anchor, menuItem[itm2Idx].text);
sendString(buf);
sendString("</FONT>\n</CENTER>\n");
}
/* ****************************** */
static void encodeWebFormURL(char *in, char *buf, int buflen) {
int i, j, c, d;
for(i=j=0; (in[i]!='\0') && (j<(buflen-4)); i++) {
c = (unsigned int)in[i];
if(isalpha(c) || isdigit(c)) {
buf[j++] = (char)c;
} else if(c == ' ') {
buf[j++] = '+';
} else {
buf[j++] = '%';
d = (c>>4) & 0x0f;
buf[j++] = (d < 10) ? '0'+d : 'A'+(d-10);
d = c & 0x0f;
buf[j++] = (d < 10) ? '0'+d : 'A'+(d-10);
}
}
buf[j] = '\0';
}
/* ****************************** */
static void decodeWebFormURL(char *buf) {
int i, j;
for(i=j=0; buf[i]!='\0'; i++,j++) {
buf[j] = buf[i];
if(buf[j] == '+') {
buf[j] = ' ';
} else if(buf[j] == '%') {
buf[j] = ((buf[i+1] >= 'A' ? ((buf[i+1] & 0xdf) - 'A')+10 : (buf[i+1] - '0')) & 0x0f) << 4 |
((buf[i+2] >= 'A' ? ((buf[i+2] & 0xdf) - 'A')+10 : (buf[i+2] - '0')) & 0x0f);
i += 2;
}
}
buf[j] = '\0';
}
/* ****************************** */
/*
Fixes below courtesy of
C C Magnus Gustavsson <[email protected]>
*/
static void addKeyIfMissing(char* key, char* value,
int encryptValue, char *userQuestion) {
datum key_data, return_data, data_data;
#ifndef WIN32
/* 14 is ok for traditional crypt, but the enhanced crypt() in FreeBSD
* (and others?) can be much larger. Just us a big buffer for ALL OSes
* in case others change too...
*/
char cpw[LEN_MEDIUM_WORK_BUFFER];
#endif
/* Check existence of user 'admin' */
key_data.dptr = key;
key_data.dsize = strlen(key_data.dptr)+1;
return_data = gdbm_fetch(myGlobals.pwFile, key_data);
if(return_data.dptr == NULL) {
char *thePw, pw1[16], pw2[16];
/* If not existing, then add user 'admin' and ask for password */
if(userQuestion != NULL) {
if(myGlobals.runningPref.daemonMode) {
/*
* We need a password for the admin user, but the user requested
* daemon mode. stdin is already detached; getpass() would fail.
*
* Courtesy of Ambrose Li <[email protected]>
*
*/
traceEvent(CONST_TRACE_FATALERROR,
"No password for admin user - please re-run ntop in non-daemon mode first");