/** \brief Plugin registration function * To be called from plugin */ void vrmr_plugin_register(struct vrmr_plugin_data *plugin_data) { struct vrmr_plugin *plugin = NULL; assert(plugin_data); if (!(plugin = malloc(sizeof(struct vrmr_plugin)))) { vrmr_error(-1, "Error", "malloc failed: %s", strerror(errno)); return; } memset(plugin, 0x00, sizeof(*plugin)); plugin->f = plugin_data; plugin->ref_cnt = 0; /* store the name of the plugin */ if (strlcpy(plugin->name, plugin_data->name, sizeof(plugin->name)) >= sizeof(plugin->name)) { vrmr_error(-1, "Internal Error", "pluginname overflow"); free(plugin); return; } /* insert into the list */ if (vrmr_list_append(&vrmr_plugin_list, plugin) == NULL) { vrmr_error(-1, "Internal Error", "vrmr_list_append() failed"); free(plugin); return; } return; }
int setup_statuslist(const int debuglvl) { /* initialize */ memset(&VuurmuurStatus, 0, sizeof(VuurmuurStatus)); memset(&VuurmuurStatus.StatusList, 0, sizeof(VuurmuurStatus.StatusList)); VuurmuurStatus.vuurmuur = 1; VuurmuurStatus.vuurmuur_log = 1; VuurmuurStatus.zones = 1; VuurmuurStatus.interfaces = 1; VuurmuurStatus.services = 1; VuurmuurStatus.rules = 1; VuurmuurStatus.shm = 1; VuurmuurStatus.backend = 1; VuurmuurStatus.config = 1; VuurmuurStatus.settings = 1; VuurmuurStatus.system = 1; /* setup the status list */ if(vrmr_list_setup(debuglvl, &VuurmuurStatus.StatusList, free_helpword) < 0) { vrmr_error(-1, VR_INTERR, "setup the statuslist failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } return(0); }
/* parse a line from the helpfile */ int read_helpline(const int debuglvl, struct vrmr_list *help_list, char *line) { char oneword[512] = ""; size_t i = 0; size_t k = 0; helpword *hw = NULL; for(i = 0, k = 0; i < StrMemLen(line); i++) { if(line[i] == ' ' || line[i] == '\n') { oneword[k] = '\0'; k = 0; /* only add a word to the list if it really contains characters */ if(StrLen(oneword) > 0) { /* get some mem for the word struct */ if(!(hw = malloc(sizeof(helpword)))) { vrmr_error(-1, VR_ERR, gettext("malloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__); return(-1); } hw->word = NULL; hw->newline = 0; hw->line_num = 0; if(!(hw->word = malloc(StrMemLen(oneword) + 1))) { vrmr_error(-1, VR_ERR, gettext("malloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__); return(-1); } (void)strlcpy(hw->word, oneword, StrMemLen(oneword) + 1); if(vrmr_list_append(debuglvl, help_list, hw) == NULL) { vrmr_error(-1, VR_INTERR, "append to list failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } } /* the newline is a special word */ if( (i == 0 && line[i] == '\n') || (i > 0 && line[i] == '\n' && (line[i-1] == '.' || line[i-1] == '?' || line[i-1] == '!' || line[i-1] == ':' || (line[i-1] == ',' && i == (StrMemLen(line) - 1) )) ) ) { /* get some mem for the word struct */ if(!(hw = malloc(sizeof(helpword)))) { vrmr_error(-1, VR_ERR, gettext("malloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__); return(-1); } hw->word = NULL; hw->newline = 1; hw->line_num = 0; if(vrmr_list_append(debuglvl, help_list, hw) == NULL) { vrmr_error(-1, VR_INTERR, "append to list (in: %s:%d).", __FUNC__, __LINE__); return(-1); } } } else { oneword[k] = line[i]; k++; } } return(0); }
static void set_wide_lines(const int debuglvl, struct vrmr_list *help_list, int width) { int line_width = 0, line_num = 1, words = 0; whelpword *hw = NULL, *next_hw = NULL; struct vrmr_list_node *d_node = NULL, *next_d_node = NULL; /* safety */ if(!help_list) { vrmr_error(-1, VR_INTERR, "parameter problem (in: %s:%d).", __FUNC__, __LINE__); return; } for(d_node = help_list->top; d_node; d_node = d_node->next, words++) { if(!(hw = d_node->data)) { vrmr_error(-1, VR_INTERR, "NULL pointer (in: %s:%d).", __FUNC__, __LINE__); return; } if(hw->word != NULL && !hw->newline) { /* get the next word */ if((next_d_node = d_node->next)) { if(!(next_hw = next_d_node->data)) { vrmr_error(-1, VR_INTERR, "NULL pointer (in: %s:%d).", __FUNC__, __LINE__); return; } /* middle in sentence, so we add a space to the word */ if(next_hw->word != NULL && next_hw->newline == 0) { /* don't add this on the current line */ if((wcslen(hw->word) + 1) >= (width - line_width)) { /* set line_width to the size of this word */ line_width = wcslen(hw->word) + 1; line_num++; hw->line_num = line_num; } /* this word fits on the current line */ else { /* add this word to the line_width */ line_width = line_width + wcslen(hw->word) + 1; hw->line_num = line_num; } } /* end of sentence, so no trailing space */ else if(next_hw->word == NULL && next_hw->newline == 1) { /* don't add this on the current line */ if(wcslen(hw->word) >= (width - line_width)) { /* set line_width to the size of this word */ line_width = wcslen(hw->word); line_num++; hw->line_num = line_num; } /* this word fits on the current line */ else { /* add this word to the line_width */ line_width = line_width + wcslen(hw->word); hw->line_num = line_num; } } else { /* undefined state */ vrmr_error(-1, VR_INTERR, "undefined state (in: %s:%d).", __FUNC__, __LINE__); return; } } /* end of text, doc has no trailing new-line */ else { /* don't add this on the current line */ if(wcslen(hw->word) >= (width - line_width)) { /* set line_width to the size of this word */ line_width = wcslen(hw->word); line_num++; hw->line_num = line_num; } /* this word fits on the current line */ else { /* add this word to the line_width */ line_width = line_width + wcslen(hw->word); hw->line_num = line_num; } } } else if(hw->word == NULL && hw->newline) { hw->line_num = line_num; line_num++; line_width = 0; } else { /* undefined state */ vrmr_error(-1, VR_INTERR, "undefined state (in: %s:%d).", __FUNC__, __LINE__); return; } } return; }
int read_wide_helpfile(const int debuglvl, struct vrmr_list *help_list, wchar_t *part) { wchar_t line[128] = L""; FILE *fp = NULL; char inrange = 0; char helpfile[256] = ""; /* safety */ if(!help_list || !part) { vrmr_error(-1, VR_INTERR, "parameter problem " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* setup the list */ if(vrmr_list_setup(debuglvl, help_list, free_helpword) < 0) { vrmr_error(-1, VR_INTERR, "vrmr_list_setup failed " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } if(utf8_mode == 1) { /* TRANSLATORS: translate this to you language code: so for 'ru' use 'vuurmuur-ru.UTF-8.hlp', for 'pt_BR' use 'vuurmuur-pt_BR.UTF-8.hlp' */ if(snprintf(helpfile, sizeof(helpfile), "%s/%s", vccnf.helpfile_location, gettext("vuurmuur.UTF-8.hlp") ) >= (int)sizeof(helpfile)) { vrmr_error(-1, VR_INTERR, "buffer too small " "for helpfile supplied at compile-time " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile)); /* open the file */ fp = fopen(helpfile, "r"); } if(fp == NULL) { if(utf8_mode == 1) vrmr_debug(__FUNC__, "opening '%s' failed: " "%s, falling back to non UTF-8 language file.", helpfile, strerror(errno)); /* UTF-8 language helpfile does not exist, try to fall back to default */ /* TRANSLATORS: translate this to you language code: so for 'ru' use 'vuurmuur-ru.hlp', for 'pt_BR' use 'vuurmuur-pt_BR.hlp' */ if(snprintf(helpfile, sizeof(helpfile), "%s/%s", vccnf.helpfile_location, gettext("vuurmuur.hlp")) >= (int)sizeof(helpfile)) { vrmr_error(-1, "Error", "buffer too small for " "helpfile supplied at compile-time " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile)); } /* open the file */ fp = fopen(helpfile, "r"); if(fp == NULL) { vrmr_debug(__FUNC__, "opening '%s' failed: %s, " "falling back to default.", helpfile, strerror(errno)); /* language helpfile does not exist, try to fall back to default */ if(snprintf(helpfile, sizeof(helpfile), "%s/vuurmuur.hlp", vccnf.helpfile_location) >= (int)sizeof(helpfile)) { vrmr_error(-1, "Error", "buffer too small for " "helpfile supplied at compile-time " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile)); if(!(fp = fopen(helpfile, "r"))) { vrmr_error(-1, VR_ERR, "%s %s: %s", STR_OPENING_FILE_FAILED, helpfile, strerror(errno)); return(-1); } } while(fgetws(line, wsizeof(line), fp) != NULL) { if(inrange) { if(wcscmp(line, L":[END]:\n") == 0) { /* implied inrange = 0; */ break; } } if(inrange) { if(read_wide_helpline(debuglvl, help_list, line) < 0) return(-1); } else { if(wcsncmp(line, part, wcslen(part)) == 0) inrange = 1; } } fclose(fp); return(0); }
char *input_box(size_t length, const char *title, const char *description) { WINDOW *ib_win = NULL; PANEL *my_panels[1]; FIELD **fields; FORM *my_form = NULL; int height, width, startx, starty, max_height, max_width, ch = 0, rows, cols, quit = 0, i; char *result_ptr = NULL, *temp_ptr = NULL; /* create a buffer with the size of 'length' */ if (!(temp_ptr = malloc(length))) { vrmr_error(-1, VR_ERR, gettext("malloc failed: %s"), strerror(errno)); return (NULL); } // set the window size getmaxyx(stdscr, max_height, max_width); height = 8; if (length < 16) { width = 37; // minimum TODO: why 37? } else if ((int)length + 8 > max_width) { free(temp_ptr); return NULL; } else { width = (int)length + 8; if ((int)StrLen(title) + 8 > width) width = (int)StrLen(title) + 8; if ((int)StrLen(description) + 8 > width) width = (int)StrLen(description) + 8; } // print on the centre of the screen starty = (max_height - height) / 2; startx = (max_width - width) / 2; // create window ib_win = create_newwin( height, width, starty, startx, title, vccnf.color_win); my_panels[0] = new_panel(ib_win); fields = (FIELD **)calloc(1 + 1, sizeof(FIELD *)); fields[0] = new_field_wrap( 1, (int)length - 1, 3, (int)(((width - length) / 2) - 2), 0, 0); set_field_back(fields[0], vccnf.color_win_rev); field_opts_off(fields[0], O_AUTOSKIP); set_field_status(fields[0], FALSE); my_form = new_form(fields); scale_form(my_form, &rows, &cols); keypad(ib_win, TRUE); set_form_win(my_form, ib_win); set_form_sub(my_form, derwin(ib_win, rows, cols, 1, 2)); post_form(my_form); mvwprintw(ib_win, 2, 4, "%s", description); mvwprintw(ib_win, 6, 4, gettext("Note: whitespaces not allowed.")); update_panels(); doupdate(); while (quit == 0) { ch = wgetch(ib_win); switch (ch) { case 27: case KEY_F(10): case 10: // enter // Go to next field form_driver_wrap(my_form, REQ_NEXT_FIELD); form_driver_wrap(my_form, REQ_END_LINE); quit = 1; break; case KEY_BACKSPACE: case 127: form_driver_wrap(my_form, REQ_PREV_CHAR); form_driver_wrap(my_form, REQ_DEL_CHAR); form_driver_wrap(my_form, REQ_END_LINE); break; case KEY_DC: form_driver_wrap(my_form, REQ_PREV_CHAR); form_driver_wrap(my_form, REQ_DEL_CHAR); form_driver_wrap(my_form, REQ_END_LINE); break; default: // If this is a normal character, it gets printed form_driver_wrap(my_form, ch); break; } } // status_print(status_win, "data: '%s' (%d)", field_buffer(fields[0], 0), // length); (void)strlcpy(temp_ptr, field_buffer(fields[0], 0), length); // get the length of the entry i = strlen(temp_ptr); while (i--) { if (isspace(temp_ptr[i])) temp_ptr[i] = '\0'; else break; } if (!(result_ptr = strdup(temp_ptr))) { vrmr_error(-1, VR_ERR, gettext("malloc failed: %s"), strerror(errno)); goto end; } if (result_ptr[0] == '\0') { free(result_ptr); result_ptr = NULL; } end: free(temp_ptr); unpost_form(my_form); free_form(my_form); free_field(fields[0]); free(fields); del_panel(my_panels[0]); destroy_win(ib_win); update_panels(); doupdate(); return (result_ptr); }
int vrmr_get_icmp_name_short(int type, int code, char *name, size_t size, int only_code) { int i=0, k=0; /* safety */ if(name == NULL) { vrmr_error(-1, "Internal Error", "parameter problem " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* type validation */ if(type < 0 || type > 255) { snprintf(name, size, "illegal icmp (%d.%d)", type, code); return(0); } #ifndef HAVE_IPV6 else if(type > 40 && type <= 255) { snprintf(name, size, "reserved icmp (%d.%d)", type, code); return(0); } #endif /* HAVE_IPV6 */ /* loop trough vrmr_icmp_types until we reach the end (-1) */ for(i=0; vrmr_icmp_types[i].type != -1; i++) { if(vrmr_icmp_types[i].type == type) { if(vrmr_icmp_types[i].has_code == 1) { /* if we called with code == -1, it means we don't want to know about the code */ if(code == -1) { (void)strlcpy(name, vrmr_icmp_types[i].short_name, size); return(0); } /* now look for the code */ for(k=0; vrmr_icmp_codes[k].type != -1; k++) { if(vrmr_icmp_codes[k].type == type) { if(vrmr_icmp_codes[k].code == code) { if(only_code == 0) { snprintf(name, size, "%s(%s)", vrmr_icmp_types[i].short_name, vrmr_icmp_codes[k].short_name); } else { (void)strlcpy(name, vrmr_icmp_codes[k].short_name, size); } return(0); } } } /* if we get here, the code was not found */ snprintf(name, size, "%s(err:%d)", vrmr_icmp_types[i].short_name, code); return(0); } else { (void)strlcpy(name, vrmr_icmp_types[i].short_name, size); return(0); } } } snprintf(name, size, "unknown icmp (%d.%d)", type, code); return(0); }
/* Actions: 1: setup 0: setdown Returncodes: 0: ok -1: error */ int vrmr_regex_setup(int action, struct vrmr_regex *reg) { if(!reg) return(-1); if(action < 0 || action > 1 || reg == NULL) { vrmr_error(-1, "Internal Error", "parameter problem " "(in: %s:%d).", __FUNC__, __LINE__); return(-1); } if(action == 1) { /* regex setup */ if(!(reg->zonename = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->zonename, VRMR_ZONE_REGEX, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->zone_part = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->zone_part, VRMR_VRMR_ZONE_REGEX_ZONEPART, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->network_part = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->network_part, VRMR_VRMR_ZONE_REGEX_NETWORKPART, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->host_part = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->host_part, VRMR_VRMR_ZONE_REGEX_HOSTPART, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->servicename = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->servicename, VRMR_SERV_REGEX, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->interfacename = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->interfacename, VRMR_IFAC_REGEX, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->macaddr = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->macaddr, VRMR_MAC_REGEX, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ if(!(reg->configline = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->configline, VRMR_CONFIG_REGEX, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* regex setup */ /* if(!(reg->comment = malloc(sizeof(regex_t)))) { vrmr_error(-1, "Internal Error", "malloc " "failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); return(-1); } if(regcomp(reg->comment, TEXTFIELD_REGEX, REG_EXTENDED) != 0) { vrmr_error(-1, "Internal Error", "regcomp() " "failed (in: %s:%d).", __FUNC__, __LINE__); return(-1); } */ } else { /* zone */ regfree(reg->zonename); free(reg->zonename); regfree(reg->zone_part); free(reg->zone_part); regfree(reg->network_part); free(reg->network_part); regfree(reg->host_part); free(reg->host_part); /* service */ regfree(reg->servicename); free(reg->servicename); /* interface */ regfree(reg->interfacename); free(reg->interfacename); /* mac */ regfree(reg->macaddr); free(reg->macaddr); /* config */ regfree(reg->configline); free(reg->configline); /* comment */ // regfree(reg->comment); // free(reg->comment); } return(0); }
int filter_input_box(const int debuglvl, struct vrmr_filter *filter) { WINDOW *ib_win = NULL; PANEL *my_panels[1]; FIELD *cur = NULL, *prev = NULL; FORM *my_form = NULL; int height = 0, width = 0, startx = 0, starty = 0, max_height = 0, max_width = 0, ch = 0, rows = 0, cols = 0, quit = 0; size_t i = 0; char not_defined = FALSE; /* init fields */ memset(&FiFi, 0, sizeof(struct FilterFields_)); /* set the window size */ getmaxyx(stdscr, max_height, max_width); height = 9; width = 48; /* print on the center of the screen */ starty = (max_height - height) / 2; startx = (max_width - width) / 2; /* create window */ ib_win = create_newwin(height, width, starty, startx, gettext("Filter"), vccnf.color_win); if(ib_win == NULL) { vrmr_error(-1, VR_ERR, gettext("creating window failed.")); return(-1); } my_panels[0] = new_panel(ib_win); if(my_panels[0] == NULL) { vrmr_error(-1, VR_ERR, gettext("creating panel failed.")); return(-1); } FiFi.n_fields = 2; FiFi.fields = (FIELD **)calloc(FiFi.n_fields + 1, sizeof(FIELD *)); if(FiFi.fields == NULL) { vrmr_error(-1, VR_ERR, gettext("calloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__); return(-1); } FiFi.string_fld = (FiFi.fields[0] = new_field(1, 31, 3, 4, 0, 0)); FiFi.check_fld = (FiFi.fields[1] = new_field(1, 1, 5, 5, 0, 0)); set_field_back(FiFi.string_fld, vccnf.color_win_rev); field_opts_off(FiFi.string_fld, O_AUTOSKIP); set_field_status(FiFi.string_fld, FALSE); set_field_buffer_wrap(debuglvl, FiFi.string_fld, 0, filter->str); set_field_back(FiFi.check_fld, vccnf.color_win); field_opts_off(FiFi.check_fld, O_AUTOSKIP); set_field_status(FiFi.check_fld, FALSE); set_field_buffer_wrap(debuglvl, FiFi.check_fld, 0, filter->neg ? "X" : " "); my_form = new_form(FiFi.fields); scale_form(my_form, &rows, &cols); keypad(ib_win, TRUE); set_form_win(my_form, ib_win); set_form_sub(my_form, derwin(ib_win, rows, cols, 1, 2)); post_form(my_form); /* XXX: we really should have a wrapper function to just print * in the middle of a window to prevent hacks like this. */ char *s = gettext("Enter filter (leave empty for no filter)"); mvwprintw(ib_win, 2, (width - StrLen(s))/2, s); mvwprintw(ib_win, 6, 6, "["); mvwprintw(ib_win, 6, 8, "]"); mvwprintw(ib_win, 6, 11, gettext("show lines that don't match")); update_panels(); doupdate(); if(!(cur = current_field(my_form))) { vrmr_error(-1, VR_INTERR, "NULL pointer (in: %s:%d).", __FUNC__, __LINE__); return(-1); } while(quit == 0) { /* draw nice markers */ draw_field_active_mark(cur, prev, ib_win, my_form, vccnf.color_win_mark|A_BOLD); not_defined = 0; /* get user input */ ch = wgetch(ib_win); if(cur == FiFi.check_fld) { if(nav_field_toggleX(debuglvl, my_form, ch) < 0) not_defined = 1; } else if(cur == FiFi.string_fld) { if(nav_field_simpletext(debuglvl, my_form, ch) < 0) not_defined = 1; } else { not_defined = 1; } /* the rest is handled here */ if(not_defined) { switch(ch) { case KEY_UP: form_driver(my_form, REQ_PREV_FIELD); form_driver(my_form, REQ_END_LINE); break; case KEY_DOWN: case 9: // tab form_driver(my_form, REQ_NEXT_FIELD); form_driver(my_form, REQ_END_LINE); break; case 10: // enter if(cur == FiFi.check_fld) { quit = 1; } else { form_driver(my_form, REQ_NEXT_FIELD); form_driver(my_form, REQ_END_LINE); } break; case 27: case KEY_F(10): case 'q': case 'Q': quit = 1; break; } } /* before we get the new 'cur', store cur in prev */ prev = cur; if(!(cur = current_field(my_form))) { vrmr_error(-1, VR_INTERR, "NULL pointer (in: %s).", __FUNC__); return(-1); } /* draw and set cursor */ wrefresh(ib_win); pos_form_cursor(my_form); } /* save here */ if(filter_save(debuglvl, filter) < 0) { vrmr_error(-1, VR_ERR, gettext("setting filter failed.")); } unpost_form(my_form); free_form(my_form); for(i=0; i < FiFi.n_fields; i++) { free_field(FiFi.fields[i]); } free(FiFi.fields); del_panel(my_panels[0]); destroy_win(ib_win); update_panels(); doupdate(); return(0); }
int script_add(const int debuglvl, VuurmuurScript *vr_script) { char found = FALSE; /* first check if the object already exists */ if( vr_script->type == VRMR_TYPE_ZONE || vr_script->type == VRMR_TYPE_NETWORK || vr_script->type == VRMR_TYPE_HOST || vr_script->type == VRMR_TYPE_GROUP) { while(vr_script->vctx.zf->list(debuglvl, vr_script->vctx.zone_backend, vr_script->bdat, &vr_script->zonetype, VRMR_BT_ZONES) != NULL) { if(vr_script->zonetype == vr_script->type && strcmp(vr_script->bdat,vr_script->name) == 0) { found = TRUE; } } if(found == TRUE) { if(vr_script->type == VRMR_TYPE_ZONE) vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "zone '%s' already exists.", vr_script->name); else if(vr_script->type == VRMR_TYPE_NETWORK) vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "network '%s' already exists.", vr_script->name); else if(vr_script->type == VRMR_TYPE_HOST) vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "host '%s' already exists.", vr_script->name); else if(vr_script->type == VRMR_TYPE_GROUP) vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "group '%s' already exists.", vr_script->name); return(VRS_ERR_ALREADY_EXISTS); } } else if(vr_script->type == VRMR_TYPE_SERVICE) { while(vr_script->vctx.sf->list(debuglvl, vr_script->vctx.serv_backend, vr_script->bdat, &vr_script->zonetype, VRMR_BT_SERVICES) != NULL) { if(strcmp(vr_script->bdat,vr_script->name) == 0) { found = TRUE; } } if(found == TRUE) { vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "service '%s' already exists.", vr_script->name); return(VRS_ERR_ALREADY_EXISTS); } } else if(vr_script->type == VRMR_TYPE_INTERFACE) { while(vr_script->vctx.af->list(debuglvl, vr_script->vctx.ifac_backend, vr_script->bdat, &vr_script->zonetype, VRMR_BT_INTERFACES) != NULL) { if(strcmp(vr_script->bdat,vr_script->name) == 0) { found = TRUE; } } if(found == TRUE) { vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "interface '%s' already exists.", vr_script->name); return(VRS_ERR_ALREADY_EXISTS); } } else if(vr_script->type == VRMR_TYPE_RULE) { while(vr_script->vctx.rf->list(debuglvl, vr_script->vctx.rule_backend, vr_script->bdat, &vr_script->zonetype, VRMR_BT_RULES) != NULL) { if(strcmp(vr_script->bdat,vr_script->name) == 0) { found = TRUE; } } if(found == TRUE) { vrmr_error(VRS_ERR_ALREADY_EXISTS, VR_ERR, "ruleset '%s' already exists.", vr_script->name); return(VRS_ERR_ALREADY_EXISTS); } } /* now add it */ if(vr_script->type == VRMR_TYPE_ZONE) { if(vr_script->vctx.zf->add(debuglvl, vr_script->vctx.zone_backend, vr_script->name, VRMR_TYPE_ZONE) < 0) { vrmr_error(-1, VR_ERR, "adding zone '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "zone '%s' added.", vr_script->name); } else if(vr_script->type == VRMR_TYPE_NETWORK) { if(vr_script->vctx.zf->add(debuglvl, vr_script->vctx.zone_backend, vr_script->name, VRMR_TYPE_NETWORK) < 0) { vrmr_error(-1, VR_ERR, "adding network '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "network '%s' added.", vr_script->name); } else if(vr_script->type == VRMR_TYPE_HOST) { if(vr_script->vctx.zf->add(debuglvl, vr_script->vctx.zone_backend, vr_script->name, VRMR_TYPE_HOST) < 0) { vrmr_error(-1, VR_ERR, "adding host '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "host '%s' added.", vr_script->name); } else if(vr_script->type == VRMR_TYPE_GROUP) { if(vr_script->vctx.zf->add(debuglvl, vr_script->vctx.zone_backend, vr_script->name, VRMR_TYPE_GROUP) < 0) { vrmr_error(-1, VR_ERR, "adding group '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "group '%s' added.", vr_script->name); } else if(vr_script->type == VRMR_TYPE_SERVICE) { if(vr_script->vctx.sf->add(debuglvl, vr_script->vctx.serv_backend, vr_script->name, VRMR_TYPE_SERVICE) < 0) { vrmr_error(-1, VR_ERR, "adding service '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "service '%s' added.", vr_script->name); } else if(vr_script->type == VRMR_TYPE_INTERFACE) { if(vr_script->vctx.af->add(debuglvl, vr_script->vctx.ifac_backend, vr_script->name, VRMR_TYPE_INTERFACE) < 0) { vrmr_error(-1, VR_ERR, "adding interface '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "interface '%s' added.", vr_script->name); } else if(vr_script->type == VRMR_TYPE_RULE) { if(vr_script->vctx.rf->add(debuglvl, vr_script->vctx.rule_backend, vr_script->name, VRMR_TYPE_RULE) < 0) { vrmr_error(-1, VR_ERR, "adding ruleset '%s' failed (in: %s:%d).", vr_script->name, __FUNC__, __LINE__); return(VRS_ERR_COMMAND_FAILED); } logchange(vr_script, "ruleset '%s' added.", vr_script->name); } else { vrmr_error(VRS_ERR_INTERNAL, VR_INTERR, "unknown type %d.", vr_script->type); return(VRS_ERR_INTERNAL); } return(0); }
/* startup_screen This is the splash-screen which calls the startup functions, like loading the plugins, zones, services etc. Returncodes: 0: ok -1: error */ int startup_screen(struct vrmr_ctx *vctx, struct vrmr_rules *rules, struct vrmr_zones *zones, struct vrmr_services *services, struct vrmr_interfaces *interfaces, struct vrmr_blocklist *blocklist, struct vrmr_regex *reg) { WINDOW *startup_win = NULL, *startup_print_win = NULL; PANEL *startup_panel[2]; int retval = 0, maxy = 0, maxx = 0, y = 0, x = 0, width = 50, heigth = 15, result = 0, config_done = 0, cnfresult = 0; int print_pan_width = 40; // get screensize and set windowlocation getmaxyx(stdscr, maxy, maxx); y = (maxy - heigth) / 2; x = (maxx - width) / 2; // create the windows and panels startup_win = create_newwin(heigth, width, y, x, "Vuurmuur_conf", vccnf.color_win_init | A_BOLD); startup_print_win = newwin(1, print_pan_width, y + heigth - 3, x + 5); wbkgd(startup_print_win, vccnf.color_win_init | A_BOLD); startup_panel[0] = new_panel(startup_win); startup_panel[1] = new_panel(startup_print_win); update_panels(); doupdate(); // print the logo: it looks a bit weird here because of escape sequences // also print version mvwprintw(startup_win, 3, 4, " \\\\ // |\\\\ //| "); mvwprintw(startup_win, 4, 4, " \\\\ // | | | | |] ||\\//|| | | | | |] "); mvwprintw(startup_win, 5, 4, " \\// \\/| \\/| |\\ || || \\/| \\/| |\\ "); mvwprintw(startup_win, 6, 4, " Config "); mvwprintw(startup_win, 7, 4, " ------------------------------------ "); mvwprintw(startup_win, 9, 3, "%s ", VUURMUUR_COPYRIGHT); mvwprintw(startup_win, 10, 3, gettext("Version: %s"), VUURMUURCONF_VERSION); mvwprintw(startup_win, 12, 4, "["); mvwprintw(startup_win, 12, 4 + print_pan_width + 1, "]"); /* initialize the vuurmuur conf config */ /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_LOAD_VUURMUUR_CONF_SETTINGS); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); while (!config_done) { result = init_vcconfig(&vctx->conf, vccnf.configfile_location, &vccnf); if (result == VRMR_CNF_E_UNKNOWN_ERR || result == VRMR_CNF_E_PARAMETER) return (-1); else if (result == VRMR_CNF_E_FILE_PERMISSION) { return (-1); } /* missing file? use defaults */ else if (result == VRMR_CNF_E_FILE_MISSING) { vcconfig_use_defaults(&vccnf); werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_LOAD_VUURMUUR_CONF_SETTINGS, STR_COK); update_panels(); doupdate(); config_done = 1; } else if (result == VRMR_CNF_E_MISSING_VAR || result == VRMR_CNF_E_ILLEGAL_VAR || result == VRMR_CNF_W_MISSING_VAR || result == VRMR_CNF_W_ILLEGAL_VAR) { if (confirm(gettext("Problem with the Vuurmuur_conf settings"), gettext("Do you want to edit the settings now?"), vccnf.color_win_note, vccnf.color_win_note_rev | A_BOLD, 1)) { /* this prompt the user with the config menu */ cnfresult = edit_vcconfig(); if (cnfresult < 0) return (-1); } else { /* if the user doesn't want to solve the problem we exit if we had an error in case of a warning, we continue */ if (result == VRMR_CNF_E_MISSING_VAR || result == VRMR_CNF_E_FILE_MISSING) return (-1); else { // TODO: print warning to warn the user that the config is // not yet ok? config_done = 1; } } } else if (result == VRMR_CNF_OK) { werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_LOAD_VUURMUUR_CONF_SETTINGS, STR_COK); update_panels(); doupdate(); config_done = 1; } else { vrmr_error(-1, VR_ERR, "unknown return code from init_vcconfig. This can't be " "good"); return (-1); } if (config_done == 0) { werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_LOAD_VUURMUUR_CONF_SETTINGS); update_panels(); doupdate(); } } /* initialize the config */ config_done = 0; werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_LOAD_VUURMUUR_CONFIG); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); while (!config_done) { result = vrmr_init_config(&vctx->conf); if (result == VRMR_CNF_E_UNKNOWN_ERR || result == VRMR_CNF_E_PARAMETER) return (-1); else if (result == VRMR_CNF_E_FILE_PERMISSION) { return (-1); } else if (result == VRMR_CNF_E_FILE_MISSING || result == VRMR_CNF_E_MISSING_VAR || result == VRMR_CNF_E_ILLEGAL_VAR || result == VRMR_CNF_W_MISSING_VAR || result == VRMR_CNF_W_ILLEGAL_VAR) { if (confirm(gettext("Problem with the Vuurmuur config"), gettext("Do you want to edit the config now?"), vccnf.color_win_note, vccnf.color_win_note_rev | A_BOLD, 1)) { /* this prompt the user with the config menu */ cnfresult = config_menu(&vctx->conf); if (cnfresult < 0) return (-1); } else { /* if the user doesn't want to solve the problem we exit if we had an error in case of a warning, we continue */ if (result == VRMR_CNF_E_MISSING_VAR || result == VRMR_CNF_E_FILE_MISSING) return (-1); else { // TODO: print warning to warn the user that the config is // not yet ok? config_done = 1; } } } else if (result == VRMR_CNF_OK) { werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_LOAD_VUURMUUR_CONFIG, STR_COK); update_panels(); doupdate(); config_done = 1; } else { vrmr_error(-1, VR_INTERR, "unknown return code from vrmr_init_config. This can't be " "good"); return (-1); } if (config_done == 0) { werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_LOAD_VUURMUUR_CONFIG); update_panels(); doupdate(); } } /* config done, so now we can use logprinting */ if (vrmr_debug_level >= LOW) vrprint.info = vuumuurconf_print_info; else vrprint.info = vrmr_logprint_info; vrprint.debug = vrmr_logprint_debug; vrprint.audit = vrmr_logprint_audit; /* print that we started */ vrmr_audit("started: effective user %s (%ld), real user %s (%ld).", vctx->user_data.username, (long)vctx->user_data.user, vctx->user_data.realusername, (long)vctx->user_data.realuser); /* now load the backends */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_LOAD_PLUGINS); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); result = vrmr_backends_load(&vctx->conf, vctx); if (result < 0) { vrmr_error(-1, VR_ERR, gettext("loading the plugins failed.")); return (-1); } werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_LOAD_PLUGINS, STR_COK); update_panels(); doupdate(); /* init services */ /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_INIT_SERVICES); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); result = vrmr_init_services(vctx, services, reg); if (result < 0) { vrmr_error(-1, VR_ERR, gettext("intializing the services failed.")); return (-1); } /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_SERVICES, STR_COK); update_panels(); doupdate(); /* init interfaces */ /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_INIT_INTERFACES); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); result = vrmr_init_interfaces(vctx, interfaces); if (result < 0) { vrmr_error(-1, VR_ERR, gettext("intializing the interfaces failed.")); return (-1); } /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_INTERFACES, STR_COK); update_panels(); doupdate(); /* init zones */ /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_INIT_ZONES); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); result = vrmr_init_zonedata(vctx, zones, interfaces, reg); if (result < 0) { vrmr_error(-1, VR_ERR, gettext("intializing the zones failed.")); return (-1); } /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_ZONES, STR_COK); update_panels(); doupdate(); /* init rules */ /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_INIT_RULES); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); result = vrmr_rules_init_list(vctx, &vctx->conf, rules, reg); if (result < 0) { /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_RULES, STR_CFAILED); update_panels(); doupdate(); } else { /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_RULES, STR_COK); update_panels(); doupdate(); } /* load the blockfile */ /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s...", STR_INIT_BLOCKLIST); update_panels(); doupdate(); if (vrmr_debug_level > LOW) sleep(1); result = vrmr_blocklist_init_list(vctx, &vctx->conf, zones, blocklist, /*load_ips*/ FALSE, /*no_refcnt*/ FALSE); if (result < 0) { /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_BLOCKLIST, STR_CFAILED); update_panels(); doupdate(); } else { /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s... %s", STR_INIT_BLOCKLIST, STR_COK); update_panels(); doupdate(); } /* try to connect to vuurmuur trough shm */ vuurmuur_shmtable = NULL; werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur...", STR_CONNECTING_TO); update_panels(); doupdate(); vuurmuur_pid = get_vuurmuur_pid("/var/run/vuurmuur.pid", &vuurmuur_shmid); if (vuurmuur_shmid > 0) { /* attach to shared memory */ vuurmuur_shmp = shmat(vuurmuur_shmid, 0, 0); if (vuurmuur_shmp == (char *)(-1)) { vrmr_error(-1, VR_ERR, gettext("attaching to shared memory failed: %s"), strerror(errno)); } else { vuurmuur_shmtable = (struct vrmr_shm_table *)vuurmuur_shmp; vuurmuur_semid = vuurmuur_shmtable->sem_id; /* now try to connect to the shared memory */ if (vrmr_lock(vuurmuur_semid)) { vuurmuur_shmtable->configtool.connected = 1; (void)snprintf(vuurmuur_shmtable->configtool.name, sizeof(vuurmuur_shmtable->configtool.name), "Vuurmuur_conf %s (user: %s)", version_string, vctx->user_data.realusername); (void)strlcpy(vuurmuur_shmtable->configtool.username, vctx->user_data.realusername, sizeof(vuurmuur_shmtable->configtool.username)); vrmr_unlock(vuurmuur_semid); werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur... %s", STR_CONNECTING_TO, STR_COK); update_panels(); doupdate(); } else { werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur... %s", STR_CONNECTING_TO, STR_CFAILED); update_panels(); doupdate(); vuurmuur_shmp = NULL; } } } else { /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur... %s", STR_CONNECTING_TO, STR_CFAILED); update_panels(); doupdate(); vuurmuur_shmp = NULL; } /* try to connect to vuurmuur trough shm */ vuurmuurlog_shmtable = NULL; /* TRANSLATORS: max 40 characters */ werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur_log...", STR_CONNECTING_TO); update_panels(); doupdate(); vuurmuurlog_pid = get_vuurmuur_pid("/var/run/vuurmuur_log.pid", &vuurmuurlog_shmid); if (vuurmuurlog_shmid > 0) { /* attach to shared memory */ vuurmuurlog_shmp = shmat(vuurmuurlog_shmid, 0, 0); if (vuurmuurlog_shmp == (char *)(-1)) { vrmr_error(-1, VR_ERR, "attaching to shared memory failed: %s", strerror(errno)); } else { vuurmuurlog_shmtable = (struct vrmr_shm_table *)vuurmuurlog_shmp; vuurmuurlog_semid = vuurmuurlog_shmtable->sem_id; vrmr_debug(LOW, "vuurmuur_log: sem_id: %d.", vuurmuurlog_semid); /* now try to connect to the shared memory */ if (vrmr_lock(vuurmuurlog_semid)) { vuurmuurlog_shmtable->configtool.connected = 1; (void)snprintf(vuurmuurlog_shmtable->configtool.name, sizeof(vuurmuurlog_shmtable->configtool.name), "Vuurmuur_conf %s (user: %s)", version_string, vctx->user_data.realusername); (void)strlcpy(vuurmuurlog_shmtable->configtool.username, vctx->user_data.realusername, sizeof(vuurmuurlog_shmtable->configtool.username)); vrmr_unlock(vuurmuurlog_semid); werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur_log... %s", STR_CONNECTING_TO, STR_COK); update_panels(); doupdate(); } else { werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur_log... %s", STR_CONNECTING_TO, STR_CFAILED); update_panels(); doupdate(); vuurmuurlog_shmp = NULL; } } } else { werase(startup_print_win); wprintw(startup_print_win, "%s Vuurmuur_log... %s", STR_CONNECTING_TO, STR_CFAILED); update_panels(); doupdate(); vuurmuurlog_shmp = NULL; } /* cleanup */ del_panel(startup_panel[0]); del_panel(startup_panel[1]); destroy_win(startup_print_win); destroy_win(startup_win); update_panels(); doupdate(); return (retval); }
int main(int argc, char *argv[]) { struct vrmr_ctx vctx; int retval = 0, optch = 0; static char optstring[] = "c:d:hVW"; struct option long_options[] = { {"configfile", required_argument, NULL, 'c'}, {"debug", required_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"wizard", no_argument, NULL, 'W'}, {0, 0, 0, 0}, }; int longopt_index = 0; int debug_level = NONE; PANEL *main_panels[5]; char *s = NULL; /* some defaults */ vuurmuur_semid = -1; vuurmuur_shmid = -1; vuurmuurlog_semid = -1; vuurmuurlog_shmid = -1; /* create the version string */ snprintf(version_string, sizeof(version_string), "%s (using libvuurmuur %s)", VUURMUURCONF_VERSION, libvuurmuur_get_version()); /* some initilization */ if (vrmr_init(&vctx, "vuurmuur_conf") < 0) exit(EXIT_FAILURE); /* settings file */ memset(vccnf.configfile_location, 0, sizeof(vccnf.configfile_location)); if (vctx.conf.etcdir[0] == '\0') (void)strlcpy(vccnf.configfile_location, VUURMUURCONF_CONFIGFILE, sizeof(vccnf.configfile_location)); else (void)snprintf(vccnf.configfile_location, sizeof(vccnf.configfile_location), "%s/vuurmuur/vuurmuur_conf.conf", vctx.conf.etcdir); #ifdef ENABLE_NLS setlocale(LC_ALL, ""); setlocale(LC_TIME, ""); setlocale(LC_MESSAGES, ""); setlocale(LC_COLLATE, ""); setlocale(LC_CTYPE, ""); setlocale(LC_MONETARY, ""); setlocale(LC_NUMERIC, ""); #endif /* check if we are in utf-8 mode */ utf8_mode = 0; if ((s = getenv("LC_ALL")) || (s = getenv("LC_CTYPE")) || (s = getenv("LANG"))) { if (strstr(s, "UTF-8")) utf8_mode = 1; } #ifdef ENABLE_NLS bindtextdomain("vuurmuur", xstr(VRMR_LOCALEDIR)); textdomain("vuurmuur"); #endif /* process commandline options */ while ((optch = getopt_long(argc, argv, optstring, long_options, &longopt_index)) != -1) { switch (optch) { case 'h': print_commandline_args(); break; /* configfile */ case 'c': if (strlcpy(vctx.conf.configfile, optarg, sizeof(vctx.conf.configfile)) >= sizeof(vctx.conf.configfile)) { vrmr_error(EXIT_FAILURE, VR_ERR, gettext("commandline argument too long for option " "-c.")); exit(EXIT_FAILURE); } break; case 'd': /* convert the debug string and check the result */ debug_level = atoi(optarg); if (debug_level < 0 || debug_level > HIGH) { vrmr_error(EXIT_FAILURE, VR_ERR, gettext("commandline debuglevel out of range.")); exit(EXIT_FAILURE); } vrmr_debug_level = debug_level; fprintf(stdout, "vuurmuur_conf: debugging enabled.\n"); fprintf(stdout, "vuurmuur_conf: debug level: %d\n", debug_level); break; case 'V': /* print version */ fprintf(stdout, "Vuurmuur_conf %s\n", version_string); fprintf(stdout, "%s\n", VUURMUUR_COPYRIGHT); exit(EXIT_SUCCESS); case 'W': { char wizard_path[512] = ""; snprintf(wizard_path, sizeof(wizard_path), "%s/scripts/vuurmuur-wizard.sh", vctx.conf.datadir); printf("Running %s...\n", wizard_path); exec_wizard(wizard_path); exit(EXIT_SUCCESS); } default: vrmr_error(EXIT_FAILURE, VR_ERR, gettext("unknown commandline option.")); exit(EXIT_FAILURE); } } /* close the STDERR_FILENO because it gives us annoying "Broken Pipe" errors on some systems with bash3. Let's see if this has negative side-effects. */ close(STDERR_FILENO); /* init vuurmuur_conf config already to get background */ (void)init_vcconfig(&vctx.conf, vccnf.configfile_location, &vccnf); /* Initialize curses */ (void)initscr(); (void)start_color(); (void)cbreak(); (void)noecho(); (void)keypad(stdscr, (bool)TRUE); setup_colors(); /* create the three main windows */ if (!(status_frame_win = create_newwin( 3, COLS, LINES - 3, 0, NULL, vccnf.color_bgd))) exit(EXIT_FAILURE); if (!(status_win = create_newwin( 1, COLS - 4, LINES - 2, 2, NULL, vccnf.color_bgd))) exit(EXIT_FAILURE); if (!(top_win = create_newwin(3, COLS, 0, 0, NULL, vccnf.color_bgd))) exit(EXIT_FAILURE); if (!(main_win = create_newwin( LINES - 6, COLS, 3, 0, NULL, vccnf.color_bgd))) exit(EXIT_FAILURE); if (!(mainlog_win = newwin(LINES - 8, COLS - 2, 4, 1))) exit(EXIT_FAILURE); (void)wbkgd(mainlog_win, vccnf.color_bgd); wattron(status_frame_win, vccnf.color_bgd); mvwprintw(status_frame_win, 0, 2, " %s ", gettext("Status")); mvwprintw(status_frame_win, 2, (int)(COLS - 4 - StrLen(vctx.user_data.realusername) - 6), " user: %s ", vctx.user_data.realusername); wattroff(status_frame_win, vccnf.color_bgd); /* Attach a panel to each window */ main_panels[0] = new_panel(top_win); main_panels[1] = new_panel(main_win); main_panels[2] = new_panel(status_win); main_panels[3] = new_panel(mainlog_win); main_panels[4] = new_panel(status_frame_win); (void)update_panels(); (void)doupdate(); /* init the vrprint functions for the Gui */ vrprint.error = vuumuurconf_print_error; vrprint.warning = vuumuurconf_print_warning; vrprint.info = vuumuurconf_print_info; if (status_print(status_win, gettext("This is Vuurmuur_conf %s, %s"), version_string, VUURMUUR_COPYRIGHT) < 0) exit(EXIT_FAILURE); /* setup the global busywin */ VrBusyWinCreate(); VrBusyWinHide(); // form_test(); /* startup_screen inits the config, loads the zones, rules, etc */ if (startup_screen(&vctx, &vctx.rules, &vctx.zones, &vctx.services, &vctx.interfaces, &vctx.blocklist, &vctx.reg) < 0) { /* failure! Lets quit. */ /* delete panels and windows */ (void)del_panel(main_panels[0]); (void)del_panel(main_panels[1]); (void)del_panel(main_panels[2]); (void)del_panel(main_panels[3]); (void)del_panel(main_panels[4]); (void)destroy_win(top_win); (void)destroy_win(main_win); (void)destroy_win(status_win); (void)destroy_win(status_frame_win); /* clear screen */ (void)refresh(); /* end ncurses mode */ (void)endwin(); exit(EXIT_FAILURE); } /* setup statuslist */ (void)setup_statuslist(); status_print(status_win, STR_READY); mm_status_checkall(&vctx, NULL, &vctx.rules, &vctx.zones, &vctx.interfaces, &vctx.services); /* main menu loop */ while (main_menu(&vctx, &vctx.rules, &vctx.zones, &vctx.interfaces, &vctx.services, &vctx.blocklist, &vctx.reg) == 1) ; /* clean up the status list */ vrmr_list_cleanup(&vuurmuur_status.StatusList); /* detach from shared memory, if we were attached */ if (vuurmuur_shmp != NULL && vuurmuur_shmp != (char *)(-1) && vuurmuur_shmtable != 0) { if (vrmr_lock(vuurmuur_semid)) { vuurmuur_shmtable->configtool.connected = 3; vrmr_unlock(vuurmuur_semid); } (void)shmdt(vuurmuur_shmp); } if (vuurmuurlog_shmp != NULL && vuurmuurlog_shmp != (char *)(-1) && vuurmuurlog_shmtable != 0) { if (vrmr_lock(vuurmuurlog_semid)) { vuurmuurlog_shmtable->configtool.connected = 3; vrmr_unlock(vuurmuurlog_semid); } (void)shmdt(vuurmuurlog_shmp); } /* destroy the global busywin */ VrBusyWinDelete(); /* delete panels and windows */ (void)del_panel(main_panels[0]); (void)del_panel(main_panels[1]); (void)del_panel(main_panels[2]); (void)del_panel(main_panels[3]); (void)del_panel(main_panels[4]); (void)destroy_win(mainlog_win); (void)destroy_win(top_win); (void)destroy_win(main_win); (void)destroy_win(status_win); (void)destroy_win(status_frame_win); /* clear screen */ (void)refresh(); /* end ncurses mode */ (void)endwin(); /* set error functions to the stdout versions */ vrprint.error = vrmr_stdoutprint_error; vrprint.warning = vrmr_stdoutprint_warning; vrprint.info = vrmr_stdoutprint_info; vrprint.debug = vrmr_stdoutprint_debug; vrprint.audit = vrmr_stdoutprint_audit; /* unload the backends */ if (vrmr_backends_unload(&vctx.conf, &vctx) < 0) { vrmr_error(-1, VR_ERR, gettext("unloading the backends failed")); retval = -1; } /* cleanup the datastructures */ (void)vrmr_list_cleanup(&vctx.blocklist.list); (void)vrmr_destroy_serviceslist(&vctx.services); (void)vrmr_destroy_zonedatalist(&vctx.zones); (void)vrmr_rules_cleanup_list(&vctx.rules); (void)vrmr_destroy_interfaceslist(&vctx.interfaces); vrmr_deinit(&vctx); return (retval); }
int script_apply(const int debuglvl, VuurmuurScript *vr_script) { /* vuurmuur */ int vuurmuur_shmid = 0; int vuurmuur_semid = -1; /*@null@*/ struct vrmr_shm_table *vuurmuur_shmtable = NULL; char *vuurmuur_shmp = NULL; /* vuurmuur_log */ int vuurmuurlog_shmid = 0; int vuurmuurlog_semid = -1; char *vuurmuurlog_shmp = NULL; /*@null@*/ struct vrmr_shm_table *vuurmuurlog_shmtable = NULL; int vuurmuur_result = 0, vuurmuurlog_result = 0; int waittime = 0; int vuurmuur_progress = 0, vuurmuurlog_progress = 0; char failed = FALSE; int retval = 0; /* try to connect to vuurmuur trough shm */ vuurmuur_shmtable = NULL; get_vuurmuur_pid("/var/run/vuurmuur.pid", &vuurmuur_shmid); if(vuurmuur_shmid > 0) { /* attach to shared memory */ vuurmuur_shmp = shmat(vuurmuur_shmid, 0, 0); if(vuurmuur_shmp == (char *)(-1)) { vrmr_error(VRS_ERR_COMMAND_FAILED, VR_ERR, "attaching to shared memory failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); } else { vuurmuur_shmtable = (struct vrmr_shm_table *)vuurmuur_shmp; vuurmuur_semid = vuurmuur_shmtable->sem_id; /* now try to connect to the shared memory */ if(vrmr_lock(vuurmuur_semid)) { vuurmuur_shmtable->configtool.connected = 1; (void)strlcpy(vuurmuur_shmtable->configtool.username, vr_script->vctx.user_data.realusername, sizeof(vuurmuur_shmtable->configtool.username)); snprintf(vuurmuur_shmtable->configtool.name, sizeof(vuurmuur_shmtable->configtool.name), "Vuurmuur_script %s (user: %s)", version_string, vr_script->vctx.user_data.realusername); vrmr_unlock(vuurmuur_semid); } else { vrmr_error(VRS_ERR_COMMAND_FAILED, VR_ERR, "connecting to Vuurmuur failed: could not lock semid."); vuurmuur_shmp = NULL; } } } else { vrmr_warning(VR_WARN, "vuurmuur not notified: connecting failed: no shmid."); vuurmuur_shmp = NULL; } /* try to connect to vuurmuur trough shm */ vuurmuurlog_shmtable = NULL; get_vuurmuur_pid("/var/run/vuurmuur_log.pid", &vuurmuurlog_shmid); if(vuurmuurlog_shmid > 0) { /* attach to shared memory */ vuurmuurlog_shmp = shmat(vuurmuurlog_shmid, 0, 0); if(vuurmuurlog_shmp == (char *)(-1)) { vrmr_error(VRS_ERR_COMMAND_FAILED, VR_ERR, "attaching to shared memory failed: %s (in: %s:%d).", strerror(errno), __FUNC__, __LINE__); } else { vuurmuurlog_shmtable = (struct vrmr_shm_table *)vuurmuurlog_shmp; vuurmuurlog_semid = vuurmuurlog_shmtable->sem_id; /* now try to connect to the shared memory */ if(vrmr_lock(vuurmuurlog_semid)) { vuurmuurlog_shmtable->configtool.connected = 1; (void)strlcpy(vuurmuurlog_shmtable->configtool.username, vr_script->vctx.user_data.realusername, sizeof(vuurmuurlog_shmtable->configtool.username)); snprintf(vuurmuurlog_shmtable->configtool.name, sizeof(vuurmuurlog_shmtable->configtool.name), "Vuurmuur_script %s (user: %s)", version_string, vr_script->vctx.user_data.realusername); vrmr_unlock(vuurmuurlog_semid); } else { vrmr_error(VRS_ERR_COMMAND_FAILED, VR_ERR, "connecting to Vuurmuur_log failed: could not lock semid."); vuurmuurlog_shmp = NULL; } } } else { vrmr_warning(VR_WARN, "vuurmuur_log not notified: connecting failed: no shmid."); vuurmuurlog_shmp = NULL; } /* handle no vuurmuur connection */ if(vuurmuur_shmtable != NULL && vuurmuur_semid != -1) { if(vrmr_lock(vuurmuur_semid)) { vuurmuur_shmtable->backend_changed = 1; vrmr_unlock(vuurmuur_semid); vuurmuur_result = VRMR_RR_NO_RESULT_YET; } } else { vuurmuur_result = VRMR_RR_READY; } /* handle no vuurmuur_log connection */ if(vuurmuurlog_shmtable != NULL && vuurmuurlog_semid != -1) { if(vrmr_lock(vuurmuurlog_semid)) { vuurmuurlog_shmtable->backend_changed = 1; vrmr_unlock(vuurmuurlog_semid); vuurmuurlog_result = VRMR_RR_NO_RESULT_YET; } } else { vuurmuurlog_result = VRMR_RR_READY; } /* wait max 60 seconds */ while (((vuurmuur_result == VRMR_RR_NO_RESULT_YET || vuurmuur_result == VRMR_RR_RESULT_ACK) || (vuurmuurlog_result == VRMR_RR_NO_RESULT_YET || vuurmuurlog_result == VRMR_RR_RESULT_ACK)) && waittime < 60000000) { if(vuurmuur_shmtable != NULL && vuurmuur_semid != -1) { if(vuurmuur_progress < 100) { if(vrmr_lock(vuurmuur_semid)) { if(vuurmuur_shmtable->reload_result != VRMR_RR_READY) { vuurmuur_result = vuurmuur_shmtable->reload_result; } vuurmuur_progress = vuurmuur_shmtable->reload_progress; vrmr_unlock(vuurmuur_semid); } } if(vuurmuur_progress == 100) { if(vuurmuur_semid == -1) { vuurmuur_result = VRMR_RR_READY; failed = 1; } else if(vrmr_lock(vuurmuur_semid)) { vuurmuur_shmtable->reload_result = VRMR_RR_RESULT_ACK; vrmr_unlock(vuurmuur_semid); if(vuurmuur_result != VRMR_RR_SUCCES && vuurmuur_result != VRMR_RR_NOCHANGES) { vuurmuur_result = VRMR_RR_READY; failed = 1; } } } } else { vuurmuur_result = VRMR_RR_READY; failed = 1; } if(vuurmuurlog_shmtable != NULL && vuurmuurlog_semid != -1) { if(vuurmuurlog_progress < 100) { if(vrmr_lock(vuurmuurlog_semid)) { if(vuurmuurlog_shmtable->reload_result != VRMR_RR_READY) { vuurmuurlog_result = vuurmuurlog_shmtable->reload_result; } vuurmuurlog_progress = vuurmuurlog_shmtable->reload_progress; vrmr_unlock(vuurmuurlog_semid); } } if(vuurmuurlog_progress == 100) { if(vuurmuurlog_semid == -1) { } else if(vrmr_lock(vuurmuurlog_semid)) { vuurmuurlog_shmtable->reload_result = VRMR_RR_RESULT_ACK; vrmr_unlock(vuurmuurlog_semid); if(vuurmuurlog_result != VRMR_RR_SUCCES && vuurmuur_result != VRMR_RR_NOCHANGES) { vuurmuurlog_result = VRMR_RR_READY; failed = 1; } } } else { vuurmuurlog_result = VRMR_RR_READY; failed = 1; } } /* no result yet, sleep 1 sec, or if the server didn't have a chance to do anything */ if( (vuurmuur_result == VRMR_RR_NO_RESULT_YET || vuurmuur_result == VRMR_RR_RESULT_ACK) || (vuurmuurlog_result == VRMR_RR_NO_RESULT_YET || vuurmuurlog_result == VRMR_RR_RESULT_ACK)) { waittime += 1000; usleep(1000); } } /* timed out */ if(vuurmuur_progress < 100) { failed = 1; } /* timed out */ if(vuurmuurlog_progress < 100) { failed = 1; } /* detach from shared memory, if we were attached */ if(vuurmuur_shmp != NULL && vuurmuur_shmp != (char *)(-1) && vuurmuur_shmtable != 0) { if(vrmr_lock(vuurmuur_semid)) { vuurmuur_shmtable->configtool.connected = 3; vrmr_unlock(vuurmuur_semid); } (void)shmdt(vuurmuur_shmp); } if(vuurmuurlog_shmp != NULL && vuurmuurlog_shmp != (char *)(-1) && vuurmuurlog_shmtable != 0) { if(vrmr_lock(vuurmuurlog_semid)) { vuurmuurlog_shmtable->configtool.connected = 3; vrmr_unlock(vuurmuurlog_semid); } (void)shmdt(vuurmuurlog_shmp); } if(failed == TRUE) retval = VRS_ERR_COMMAND_FAILED; return(retval); }
int tell_textdir(void *backend, const char *name, const char *question, const char *answer, int overwrite, enum vrmr_objecttypes type) { int retval = 0; char *file_location = NULL; char line[512] = "", *line_ptr = NULL, *tmp_line_ptr = NULL; int i = 0, found = 0, skip = 0; FILE *fp = NULL; struct vrmr_list storelist; struct vrmr_list_node *d_node = NULL; assert(backend && name && question && answer); vrmr_debug(HIGH, "question: %s, answer: %s, name: %s, overwrite: %d, type: %d", question, answer, name, overwrite, type); struct textdir_backend *tb = (struct textdir_backend *)backend; if (!tb->backend_open) { vrmr_error(-1, "Error", "backend not opened yet"); return (-1); } /* only uppercase allowed */ while (question[i]) { if ((question[i] >= 'a') && (question[i] <= 'z')) return (-1); ++i; } if (!(file_location = get_filelocation(backend, name, type))) return (-1); /* first open the file for reading */ if (!(fp = vuurmuur_fopen(tb->cfg, file_location, "r"))) { vrmr_error(-1, "Error", "unable to open file '%s' for reading: %s.", file_location, strerror(errno)); free(file_location); return (-1); } /* initialize the store list */ vrmr_list_setup(&storelist, free); /* loop trough the current file */ while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) { skip = 0; size_t line_size = sizeof(line); if (!(line_ptr = malloc(line_size))) { vrmr_error(-1, "Error", "malloc failed: %s", strerror(errno)); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); fclose(fp); return (-1); } if (strncmp(question, line, strlen(question)) == 0 && line[strlen(question)] == '=') { if (overwrite && !found) { snprintf(line_ptr, line_size, "%s=\"%s\"\n", question, answer); found = 1; } else if (overwrite && found) { skip = 1; } else { (void)strlcpy(line_ptr, line, line_size); found = 1; } } else { (void)strlcpy(line_ptr, line, line_size); } /* now append the line to the storelist, except if we were told to skip this one. Then just free the data. */ if (!skip) { if (vrmr_list_append(&storelist, line_ptr) == NULL) { vrmr_error(-1, "Internal Error", "inserting line into temporary storage list failed"); /* cleanup */ vrmr_list_cleanup(&storelist); free(line_ptr); free(file_location); fclose(fp); return (-1); } } else { /* free and null */ free(line_ptr); line_ptr = NULL; } } /* if we are not overwriting and the type of data is already found somewhere, we try to insert is just below the last one. */ if (!overwrite && found) { if (!(line_ptr = malloc(sizeof(line)))) { vrmr_error(-1, "Error", "malloc failed: %s", strerror(errno)); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); fclose(fp); return (-1); } /* assemble the line */ snprintf(line_ptr, sizeof(line), "%s=\"%s\"\n", question, answer); /* loop the list bottom up so we match the last one first */ for (d_node = storelist.bot; d_node; d_node = d_node->prev) { if (!(tmp_line_ptr = d_node->data)) { vrmr_error(-1, "Internal Error", "NULL pointer"); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); free(line_ptr); fclose(fp); return (-1); } /* check if the line is the same. If so insert after it. */ if (strncmp(question, tmp_line_ptr, strlen(question)) == 0) { if (vrmr_list_insert_after(&storelist, d_node, line_ptr) == NULL) { vrmr_error(-1, "Internal Error", "inserting line into temporary storage list " "failed"); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); free(line_ptr); fclose(fp); return (-1); } /* after inserting we're done */ line_ptr = NULL; break; } /* Don't free line_ptr yet, because it might be used again in the * next iteration */ } /* we no longer need these */ tmp_line_ptr = NULL; free(line_ptr); line_ptr = NULL; } /* if its not found, we insert it at the end of the list */ if (found == 0) { /* first alloc */ if (!(line_ptr = malloc(sizeof(line)))) { vrmr_error(-1, "Error", "malloc failed: %s.", strerror(errno)); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); fclose(fp); return (-1); } snprintf(line_ptr, sizeof(line), "%s=\"%s\"\n", question, answer); /* append into the list */ if (vrmr_list_append(&storelist, line_ptr) == NULL) { vrmr_error(-1, "Internal Error", "inserting line into temporary storage list failed"); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); free(line_ptr); fclose(fp); return (-1); } /* we no longer need this */ line_ptr = NULL; } /* close the file */ (void)fclose(fp); /* now open the file for writing */ if (!(fp = vuurmuur_fopen(tb->cfg, file_location, "w+"))) { vrmr_error(-1, "Error", "unable to open file '%s' for writing: %s", file_location, strerror(errno)); /* cleanup */ vrmr_list_cleanup(&storelist); free(file_location); return (-1); } /* print the list into the file */ for (d_node = storelist.top; d_node; d_node = d_node->next) { if (d_node->data == NULL) continue; fprintf(fp, "%s", (char *)d_node->data); } (void)fclose(fp); /* destroy the temp storage */ vrmr_list_cleanup(&storelist); free(file_location); return (retval); }
static void do_wide_print(const int debuglvl, WINDOW *printwin, struct vrmr_list *list, int start_print, int end_print) { whelpword *hw = NULL, *next_hw = NULL; struct vrmr_list_node *d_node = NULL, *next_d_node = NULL; /* print the text */ for(d_node = list->top; d_node; d_node = d_node->next) { if(!(hw = d_node->data)) { vrmr_error(-1, VR_INTERR, "NULL pointer " "(in: %s:%d).", __FUNC__, __LINE__); return; } if(hw->line_num >= start_print && hw->line_num <= end_print) { if(hw->word != NULL) { if((next_d_node = d_node->next)) { if(!(next_hw = next_d_node->data)) { vrmr_error(-1, VR_INTERR, "NULL pointer " "(in: %s:%d).", __FUNC__, __LINE__); return; } /* end of sentence */ if(next_hw->word == NULL && next_hw->newline == 1) { wprintw(printwin, "%ls", hw->word); } /* middle in sentence */ else if(next_hw->line_num > hw->line_num) { wprintw(printwin, "%ls\n", hw->word); } /* next one is only a dot */ else if(next_hw->word != NULL && wcscmp(next_hw->word, L".") == 0) { wprintw(printwin, "%ls", hw->word); } else { wprintw(printwin, "%ls ", hw->word); } } /* end of text */ else { wprintw(printwin, "%ls", hw->word); } } /* new line */ else { wprintw(printwin, "\n"); } } } }
void print_list(const int debuglvl, struct vrmr_list *list, char *title, int height, int width, int starty, int startx, char utf8) { WINDOW *boxwin = NULL, *printwin = NULL; PANEL *panel[2]; int ch; helpword *hw = NULL; size_t start_print = 1, end_print = 1; char done = 0; int i = 0; size_t size = 0; end_print = (size_t)height - 2; if(!(boxwin = create_newwin(height, width, starty, startx, title, vccnf.color_win))) { vrmr_error(-1, VR_ERR, "could not create window (in: %s:%d).", __FUNC__, __LINE__); return; } if(!(panel[0] = new_panel(boxwin))) { vrmr_error(-1, VR_ERR, "could not create panel (in: %s:%d).", __FUNC__, __LINE__); return; } if(!(printwin = newwin(height-2, width-4, starty+1, startx+2))) { vrmr_error(-1, VR_ERR, "could not create window (in: %s:%d).", __FUNC__, __LINE__); return; } (void)wbkgd(printwin, vccnf.color_win); if(!(panel[1] = new_panel(printwin))) { vrmr_error(-1, VR_ERR, "could not create panel (in: %s:%d).", __FUNC__, __LINE__); return; } keypad(printwin, TRUE); size = StrLen(gettext("Press <F10> to close this window.")); mvwprintw(boxwin, height-1, (int)(width-size)/2, " %s ", gettext("Press <F10> to close this window.")); wrefresh(boxwin); while(!done) { werase(printwin); if(list->len == 0) { wprintw(printwin, gettext("The requested helptext was not found.\n")); } #ifdef USE_WIDEC if(utf8 == UTF8_TRUE) do_wide_print(debuglvl, printwin, list, start_print, end_print); else #endif /* USE_WIDEC */ do_print(debuglvl, printwin, list, start_print, end_print); update_panels(); doupdate(); /* get user input */ ch = wgetch(printwin); switch(ch) { case KEY_DOWN: if(list->len > 0) { if(!(hw = list->bot->data)) { vrmr_error(-1, VR_INTERR, "NULL pointer (in: %s:%d).", __FUNC__, __LINE__); return; } if(end_print < hw->line_num) { start_print++; end_print++; } } break; case KEY_UP: if(list->len > 0) { if(start_print > 1) { start_print--; end_print--; } } break; case KEY_PPAGE: if(list->len > 0) { i = (height - 2)/3; while((start_print - i) < 1) i--; start_print = start_print - i; end_print = end_print - i; } break; case KEY_NPAGE: if(list->len > 0) { i = (height - 2)/3; if(!(hw = list->bot->data)) { vrmr_error(-1, VR_INTERR, "NULL pointer (in: %s:%d).", __FUNC__, __LINE__); return; } while((end_print + i) > hw->line_num) i--; start_print = start_print + i; end_print = end_print + i; } break; default: done = 1; break; } } del_panel(panel[0]); del_panel(panel[1]); destroy_win(printwin); destroy_win(boxwin); update_panels(); doupdate(); return; }
static int filter_save(const int debuglvl, struct vrmr_filter *filter) { size_t i = 0; char filter_str[48] = ""; /* safety */ if(filter == NULL) { vrmr_error(-1, VR_INTERR, "parameter problem (in: %s:%d).", __FUNC__, __LINE__); return(-1); } /* check for changed fields */ for(i = 0; i < FiFi.n_fields; i++) { if(FiFi.fields[i] == FiFi.check_fld) { if(strncmp(field_buffer(FiFi.fields[i], 0), "X", 1) == 0) filter->neg = TRUE; else filter->neg = FALSE; if(debuglvl >= MEDIUM) vrmr_debug(__FUNC__, "filter->neg is now %s.", filter->neg ? "TRUE" : "FALSE"); } /* ipaddress field */ else if(FiFi.fields[i] == FiFi.string_fld) { if(!(copy_field2buf(filter->str, field_buffer(FiFi.fields[i], 0), sizeof(filter->str)))) return(-1); if(debuglvl >= MEDIUM) vrmr_debug(__FUNC__, "filter field changed: %s.", filter->str); /* new str */ if(StrLen(filter->str) > 0) { if(filter->reg_active == TRUE) { /* first remove old regex */ regfree(&filter->reg); /* set reg_active to false */ filter->reg_active = FALSE; } snprintf(filter_str, sizeof(filter_str), ".*%s.*", filter->str); /* create the new regex */ if(regcomp(&filter->reg, filter_str, REG_EXTENDED) != 0) { vrmr_error(-1, VR_INTERR, "setting up the regular expression with regcomp failed. Disabling filter."); return(-1); } /* set reg_active to true */ filter->reg_active = TRUE; } /* empty field, remove regex */ if(StrLen(filter->str) == 0 && filter->reg_active == TRUE) { /* first remove old regex */ regfree(&filter->reg); /* set reg_active to false */ filter->reg_active = FALSE; } } else { vrmr_error(-1, VR_INTERR, "unknown field (in: %s:%d).", __FUNC__, __LINE__); return(-1); } } return(0); }