1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
6 fltedit.c - the filter editing Facility
10 #include "iptraf-ng-compat.h"
12 #include "tui/labels.h"
13 #include "tui/menurt.h"
14 #include "tui/msgboxes.h"
15 #include "tui/winops.h"
27 void init_filter_table(struct filterlist
*fl
)
29 fl
->head
= fl
->tail
= NULL
;
33 * Loads the filter from the filter file
36 int loadfilter(char *filename
, struct filterlist
*fl
, int resolve
)
44 init_filter_table(fl
);
46 pfd
= open(filename
, O_RDONLY
);
49 write_error("Error opening IP filter data file");
54 fe
= xmalloc(sizeof(struct filterent
));
55 br
= read(pfd
, &(fe
->hp
), sizeof(struct hostparams
));
61 nametoaddr(fe
->hp
.s_fqdn
, &resolv_err
);
63 nametoaddr(fe
->hp
.d_fqdn
, &resolv_err
);
70 fe
->smask
= inet_addr(fe
->hp
.s_mask
);
71 fe
->dmask
= inet_addr(fe
->hp
.d_mask
);
73 if (fl
->head
== NULL
) {
75 fe
->prev_entry
= NULL
;
77 fl
->tail
->next_entry
= fe
;
78 fe
->prev_entry
= fl
->tail
;
80 fe
->next_entry
= NULL
;
94 void savefilter(char *filename
, struct filterlist
*fl
)
96 struct filterent
*fe
= fl
->head
;
100 pfd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
103 bw
= write(pfd
, &(fe
->hp
), sizeof(struct hostparams
));
106 tui_error(ANYKEY_MSG
, "Unable to save filter changes");
115 void print_hostparam_line(struct filterent
*fe
, int idx
, WINDOW
* win
, int attr
)
117 struct in_addr binmask
;
122 mvwprintw(win
, idx
, 0, "%78c", ' ');
124 mvwaddnstr(win
, idx
, 1, fe
->hp
.s_fqdn
, 20);
125 if (inet_aton(fe
->hp
.s_mask
, &binmask
) == 0)
126 inet_aton("255.255.255.255", &binmask
);
128 wprintw(win
, "/%u", cidr_get_maskbits(binmask
.s_addr
));
129 if (fe
->hp
.sport2
== 0)
130 wprintw(win
, ":%u", fe
->hp
.sport1
);
132 wprintw(win
, ":%u-%u", fe
->hp
.sport1
, fe
->hp
.sport2
);
135 if (fe
->hp
.match_opposite
!= 'Y')
140 mvwaddnstr(win
, idx
, 38, fe
->hp
.d_fqdn
, 15);
142 if (inet_aton(fe
->hp
.d_mask
, &binmask
) == 0)
143 inet_aton("255.255.255.255", &binmask
);
145 wprintw(win
, "/%u", cidr_get_maskbits(binmask
.s_addr
));
147 if (fe
->hp
.dport2
== 0)
148 wprintw(win
, ":%u", fe
->hp
.dport1
);
150 wprintw(win
, ":%u-%u", fe
->hp
.dport1
, fe
->hp
.dport2
);
152 mvwprintw(win
, idx
, 76, "%c", toupper(fe
->hp
.reverse
));
156 void update_hp_screen(struct filterent
*firstvisible
, WINDOW
* win
)
158 struct filterent
*ftmp
= firstvisible
;
161 wattrset(win
, STDATTR
);
162 if (firstvisible
== NULL
) {
163 mvwprintw(win
, 0, 0, "%78c", ' ');
169 for (i
= 0; i
<= 12; i
++) {
171 print_hostparam_line(ftmp
, i
, win
, STDATTR
);
172 ftmp
= ftmp
->next_entry
;
174 mvwprintw(win
, i
, 0, "%78c", ' ');
181 void modify_host_parameters(struct filterlist
*fl
)
187 struct filterent
*fe
;
188 struct filterent
*ftemp
;
190 struct filterent
*firstvisible
= NULL
;
192 unsigned int idx
= 0;
193 int endloop_local
= 0;
205 bwin
= newwin(15, 80, (LINES
- 15) / 2, (COLS
- 80) / 2);
207 bpanel
= new_panel(bwin
);
208 win
= newwin(13, 78, (LINES
- 13) / 2, (COLS
- 78) / 2);
209 panel
= new_panel(win
);
211 wattrset(bwin
, BOXATTR
);
212 tx_box(bwin
, ACS_VLINE
, ACS_HLINE
);
214 mvwprintw(bwin
, 0, 2, " Source ");
215 mvwprintw(bwin
, 0, 38, " Destination ");
216 mvwprintw(bwin
, 0, 74, " I/E ");
218 mvwprintw(bwin
, 14, 1, " Filter Data ");
221 wattrset(win
, STDATTR
);
225 tx_printkeyhelp("Up/Down", "-move ptr ", stdscr
, HIGHATTR
,
227 tx_printkeyhelp("I", "-insert ", stdscr
, HIGHATTR
, STATUSBARATTR
);
228 tx_printkeyhelp("A", "-add to list ", stdscr
, HIGHATTR
, STATUSBARATTR
);
229 tx_printkeyhelp("D", "-delete ", stdscr
, HIGHATTR
, STATUSBARATTR
);
230 tx_printkeyhelp("Enter", "-edit ", stdscr
, HIGHATTR
, STATUSBARATTR
);
231 tx_printkeyhelp("X/Ctrl+X", "-exit", stdscr
, HIGHATTR
, STATUSBARATTR
);
236 firstvisible
= fl
->head
;
238 update_hp_screen(firstvisible
, win
);
248 print_hostparam_line(fe
, idx
, win
, BARSTDATTR
);
254 print_hostparam_line(fe
, idx
, win
, STDATTR
);
258 if (fl
->head
!= NULL
) {
259 if (fe
->prev_entry
!= NULL
) {
266 firstvisible
->prev_entry
;
273 if (fl
->head
!= NULL
) {
274 if (fe
->next_entry
!= NULL
) {
281 firstvisible
->next_entry
;
290 ftemp
= xmallocz(sizeof(struct filterent
));
292 gethostparams(&(ftemp
->hp
), "", "", "", "", "", "", "",
293 "", "I", "N", &gh_aborted
);
300 if (fl
->head
== NULL
) {
301 ftemp
->next_entry
= ftemp
->prev_entry
= NULL
;
302 fl
->head
= fl
->tail
= ftemp
;
303 firstvisible
= fl
->head
;
306 ftemp
->next_entry
= fe
;
307 ftemp
->prev_entry
= fe
->prev_entry
;
310 * Point firstvisible at new entry if we inserted at the
314 if (ftemp
->prev_entry
== NULL
) {
316 firstvisible
= ftemp
;
318 fe
->prev_entry
->next_entry
= ftemp
;
320 fe
->prev_entry
= ftemp
;
323 if (ftemp
->next_entry
== NULL
)
327 update_hp_screen(firstvisible
, win
);
332 ftemp
= xmallocz(sizeof(struct filterent
));
334 gethostparams(&(ftemp
->hp
), "", "", "", "", "", "", "",
335 "", "I", "N", &gh_aborted
);
343 * Add new node to the end of the list (or to the head if the
346 if (fl
->tail
!= NULL
) {
347 fl
->tail
->next_entry
= ftemp
;
348 ftemp
->prev_entry
= fl
->tail
;
352 ftemp
->prev_entry
= ftemp
->next_entry
= NULL
;
353 firstvisible
= fl
->head
;
358 ftemp
->next_entry
= NULL
;
360 update_hp_screen(firstvisible
, win
);
365 if (fl
->head
!= NULL
) {
367 * Move firstvisible down if it's pointing to the target
371 if (firstvisible
== fe
)
372 firstvisible
= fe
->next_entry
;
375 * Detach target node from list.
377 if (fe
->next_entry
!= NULL
)
378 fe
->next_entry
->prev_entry
=
381 fl
->tail
= fe
->prev_entry
;
383 if (fe
->prev_entry
!= NULL
)
384 fe
->prev_entry
->next_entry
=
387 fl
->head
= fe
->next_entry
;
390 * Move pointer up if we're deleting the last entry.
391 * The list tail pointer has since been moved to the
394 if (fe
->prev_entry
== fl
->tail
) {
395 ftemp
= fe
->prev_entry
;
398 * Move screen pointer up. Really adjust the index if
399 * the pointer is anywhere below the top of the screen.
405 * Otherwise scroll the list down, and adjust the
406 * firstvisible pointer to point to the entry
407 * previous to the target.
410 firstvisible
= ftemp
;
415 * If we reach this point, we're deleting from before
416 * the tail of the list. In that case, we point the
417 * screen pointer at the entry following the target.
419 ftemp
= fe
->next_entry
;
423 update_hp_screen(firstvisible
, win
);
428 sprintf(s_portstr1
, "%u", fe
->hp
.sport1
);
429 sprintf(s_portstr2
, "%u", fe
->hp
.sport2
);
430 sprintf(d_portstr1
, "%u", fe
->hp
.dport1
);
431 sprintf(d_portstr2
, "%u", fe
->hp
.dport2
);
432 inexstr
[0] = toupper(fe
->hp
.reverse
);
434 matchop
[0] = toupper(fe
->hp
.match_opposite
);
437 gethostparams(&(fe
->hp
), fe
->hp
.s_fqdn
,
438 fe
->hp
.s_mask
, s_portstr1
,
439 s_portstr2
, fe
->hp
.d_fqdn
,
440 fe
->hp
.d_mask
, d_portstr1
,
441 d_portstr2
, inexstr
, matchop
,
444 update_hp_screen(firstvisible
, win
);
463 } while (!endloop_local
);
473 /* remove a currently applied filter from memory */
474 void destroyfilter(struct filterlist
*fl
)
476 struct filterent
*fe
= fl
->head
;
479 struct filterent
*cfe
= fe
->next_entry
;
490 void definefilter(int *aborted
)
492 struct filterfileent ffile
;
494 struct filterlist fl
;
499 get_filter_description(ffile
.desc
, aborted
, "");
504 genname(time(NULL
), fntemp
);
507 open(get_path(T_WORKDIR
, fntemp
), O_CREAT
| O_WRONLY
| O_TRUNC
,
510 tui_error(ANYKEY_MSG
, "Cannot create filter data file");
518 open(OTHIPFLNAME
, O_CREAT
| O_WRONLY
| O_APPEND
, S_IRUSR
| S_IWUSR
);
524 strcpy(ffile
.filename
, fntemp
);
525 bw
= write(pfd
, &ffile
, sizeof(struct filterfileent
));
531 init_filter_table(&fl
);
532 modify_host_parameters(&fl
);
533 savefilter(get_path(T_WORKDIR
, fntemp
), &fl
);
538 * Edit an existing filter
540 void editfilter(int *aborted
)
542 char filename
[FLT_FILENAME_MAX
];
543 struct filterlist fl
;
544 struct ffnode
*flist
;
545 struct ffnode
*ffile
;
546 struct filterfileent
*ffe
;
548 if (loadfilterlist(&flist
) == 1) {
550 destroyfilterlist(flist
);
553 pickafilter(flist
, &ffile
, aborted
);
556 destroyfilterlist(flist
);
561 get_filter_description(ffe
->desc
, aborted
, ffe
->desc
);
564 destroyfilterlist(flist
);
567 strncpy(filename
, get_path(T_WORKDIR
, ffe
->filename
),
568 FLT_FILENAME_MAX
- 1);
570 if (loadfilter(filename
, &fl
, FLT_DONTRESOLVE
))
573 modify_host_parameters(&fl
);
575 save_filterlist(flist
); /* This also destroys it */
576 savefilter(filename
, &fl
);
581 * Delete a filter record from the disk
584 void delfilter(int *aborted
)
586 struct ffnode
*fltfile
;
587 struct ffnode
*fltlist
;
589 if (loadfilterlist(&fltlist
) == 1) {
592 destroyfilterlist(fltlist
);
595 pickafilter(fltlist
, &fltfile
, aborted
);
600 unlink(get_path(T_WORKDIR
, fltfile
->ffe
.filename
));
602 if (fltfile
->prev_entry
== NULL
) {
603 fltlist
= fltlist
->next_entry
;
605 fltlist
->prev_entry
= NULL
;
607 fltfile
->prev_entry
->next_entry
= fltfile
->next_entry
;
609 if (fltfile
->next_entry
!= NULL
)
610 fltfile
->next_entry
->prev_entry
= fltfile
->prev_entry
;
615 save_filterlist(fltlist
);