int streamdecode_process(struct stream_state *s, size_t count, double samples[count]) { unsigned window_size = WINDOW_SIZE(&s->as); for (unsigned i = 0; i < count; i++) { s->gbltick++; s->tick++; filter_put(s->chan, samples[i]); double bandpassed = filter_get(s->chan); for (int b = 0; b < 2; b++) { filter_put(s->bit[b], bandpassed); double *energy = &s->energy[b]; double *trailing = &s->ehist[b][(i + window_size - 1) % window_size]; *energy -= *trailing; double bitval = filter_get(s->bit[b]); double term = bitval * bitval; *trailing = term; *energy += term; } // drop the first WINDOW_SIZE samples to make energy readings meaningful if (s->gbltick > window_size) if (state_update(s)) return -1; } return 0; }
int INTERPOSE(socket)(int domain, int type, int protocol) { __real_socket_init(); const bool bypass_filter = getenv("SIXJACK_BYPASS") != NULL; int ret = 0; int ret_errno = 0; bool bypass_call = false; FilterReplyResultBase rb = { .pre = true, .ret = &ret, .ret_errno = &ret_errno, .fd = -1 }; if (bypass_filter == false && (rb.filter = filter_get()) && filter_apply(&rb, &domain, &type, &protocol) == FILTER_REPLY_BYPASS) { bypass_call = true; } if (bypass_call == false) { ret = __real_socket(domain, type, protocol); ret_errno = errno; } if (bypass_filter == false) { rb.fd = ret; rb.pre = false; filter_apply(&rb, &domain, &type, &protocol); } errno = ret_errno; return ret; }
ssize_t INTERPOSE(recvmsg)(int fd, struct msghdr *msg, int flags) { __real_recvmsg_init(); const bool bypass_filter = getenv("SIXJACK_BYPASS") != NULL || is_socket(fd) == false; struct sockaddr_storage sa_local, *sa_local_ = &sa_local; socklen_t sa_local_len; get_sock_info(fd, &sa_local_, &sa_local_len, NULL, NULL); int ret = 0; int ret_errno = 0; bool bypass_call = false; size_t nbyte = (size_t) 0U; struct iovec * const vecs = msg->msg_iov; size_t i_vecs = 0U; while (i_vecs < (size_t) msg->msg_iovlen) { assert(SIZE_MAX - nbyte >= vecs[i_vecs].iov_len); nbyte += vecs[i_vecs].iov_len; i_vecs++; } size_t new_nbyte = nbyte; FilterReplyResultBase rb = { .pre = true, .ret = &ret, .ret_errno = &ret_errno, .fd = fd }; if (bypass_filter == false && (rb.filter = filter_get()) && filter_apply(&rb, sa_local_, sa_local_len, msg, &new_nbyte, &flags) == FILTER_REPLY_BYPASS) { bypass_call = true; } if (bypass_call == false) { ssize_t ret_ = __real_recvmsg(fd, msg, flags); ret_errno = errno; ret = (int) ret_; assert((ssize_t) ret_ == ret); } if (bypass_filter == false) { new_nbyte = ret; rb.pre = false; filter_apply(&rb, sa_local_, sa_local_len, msg, &new_nbyte, &flags); } errno = ret_errno; return ret; }
/* Return 0 to allow, non-zero to block */ int filter_domain(const char *host, const char *aclname, char **status) { struct filter_list *f; struct filter_rulelist *p; int result; if (!fl || !filterlist_initialized || !(f = filter_get(aclname))) goto COMMON_EXIT; DEBUG2("%s: got filterlist for %s", __func__, aclname); for (p = f->rules; p; p = p->next) { switch (p->type) { case FL_ALLOW: case FL_DENY: result = regexec(p->cpat, host, (size_t) 0, (regmatch_t *) 0, 0); if (result == 0) { DEBUG2("%s: match: %s", __func__, p->pat); if (p->type == FL_ALLOW) return 0; else return 1; } break; case FL_OFCD: result = filter_ofcd((const unsigned char *) p->pat, host, status); if (result == 0) return 2; else if (result == 1) return 0; break; default: DEBUG2("%s: filter type %d not yet supported", p->type); } } COMMON_EXIT: if (config.default_policy == FILTER_ALLOW) return 0; else return 1; }
ssize_t INTERPOSE(read)(int fd, void *buf, size_t nbyte) { __real_read_init(); const bool bypass_filter = getenv("SIXJACK_BYPASS") != NULL || is_socket(fd) == false; struct sockaddr_storage sa_local, *sa_local_ = &sa_local; struct sockaddr_storage sa_remote, *sa_remote_ = &sa_remote; socklen_t sa_local_len, sa_remote_len; get_sock_info(fd, &sa_local_, &sa_local_len, &sa_remote_, &sa_remote_len); int ret = 0; int ret_errno = 0; bool bypass_call = false; size_t new_nbyte = nbyte; FilterReplyResultBase rb = { .pre = true, .ret = &ret, .ret_errno = &ret_errno, .fd = fd }; if (bypass_filter == false && (rb.filter = filter_get()) && filter_apply(&rb, sa_local_, sa_local_len, sa_remote_, sa_remote_len, NULL, &new_nbyte) == FILTER_REPLY_BYPASS) { bypass_call = true; } if (bypass_call == false) { ssize_t ret_ = __real_read(fd, buf, new_nbyte); ret_errno = errno; ret = (int) ret_; assert((ssize_t) ret_ == ret); } if (bypass_filter == false) { rb.pre = false; filter_apply(&rb, sa_local_, sa_local_len, sa_remote_, sa_remote_len, buf, &new_nbyte); } errno = ret_errno; return ret; }
/* * Initializes a linked list of strings containing hosts/urls to be filtered */ void filter_init(void) { struct filter_list *p = NULL, *q; struct filter_s *f; if (!config.filters) { filterlist_initialized = 1; return; } if (!fl && !filterlist_initialized) { int i = 0; while ((f = config.filters[i++])) { /* are there already rules for this acl? */ if (!(q = filter_get(f->aclname))) { log_message(LOG_INFO, "%s: New Filter for %s", __func__, f->aclname); if (!p) { /* head of list */ p = fl = safecalloc(1, sizeof(struct filter_list)); } else { /* next entry */ p->next = safecalloc(1, sizeof(struct filter_list)); p = p->next; } p->aclname = safestrdup(f->aclname); q = p; } else { log_message(LOG_INFO, "%s: Found Filter for %s", __func__, f->aclname); } filter_read(f->expression, &q->rules); } filterlist_initialized = 1; } }
int ui_run() { int ch; int load_worked = 0; #ifdef DEBUG int debug_i = 0; #endif time_base = time(NULL); while (1) { can_resize = TRUE; if (should_resize) { ui_resize(); } ch = getch(); ui_statusline_clear(); can_resize = FALSE; if ((time_base < (time(NULL) - (options->passphrase_timeout * 60))) && options->passphrase_timeout != 0 && tolower(ch) != 'q') { folder_write_file(); folder_free_all(); ui_statusline_msg("Passphrase has timed out and you must enter it again."); getch(); load_worked = folder_read_file(); if (load_worked != 0) { ui_statusline_msg("Error - unable to re-load the password file!"); break; } if (search_results != NULL) search_remove(); time_base = time(NULL); continue; } switch (ch) { case 'Q': case 'q': if (search_results != NULL) search_remove(); else if (action_list_at_top_level()) return 0; break; case '?': ui_display_help(); break; case KEY_PPAGE: uilist_page_up(); break; case KEY_NPAGE: uilist_page_down(); break; case KEY_UP: case 'k': uilist_up(); break; case KEY_DOWN: case 'j': uilist_down(); break; case 'A': if (!options->readonly) action_list_add_sublist(); else statusline_readonly(); break; case 'U': action_list_up_one_level(); break; case 'r': if (!options->readonly) { action_list_rename(); folder_write_file(); } else { statusline_readonly(); } break; case 'a': if (!options->readonly) { action_list_add_pw(); folder_write_file(); } else { statusline_readonly(); } break; case 'e': case ' ': case 13: /* return/enter key */ action_list_select_item(); break; case 'x': action_list_mark(); break; case 'B': action_list_copy_username(); break; case 'C': action_list_copy_pw(); break; case 'd': case 0x14A: /* DEL key */ if (!options->readonly) action_list_delete_item(); else statusline_readonly(); break; case 'm': if (!options->readonly) action_list_move_item(); else statusline_readonly(); break; case 'o': action_edit_options(); break; case 0x17: /* control-w */ if (!options->readonly) folder_write_file(); else statusline_readonly(); break; case 0x12: /* control-r */ action_list_read_file(); break; case 0x07: /* control-g */ pwgen_indep(); break; case 0x06: /* control-f */ gnupg_forget_passphrase(); break; case 0x0C: /* control-l */ ui_refresh_windows(); break; case '/': case 'F': search_get(); break; case 'f': filter_get(); break; case 'E': action_list_export(); break; case 'I': if (!options->readonly) { folder_import_passwd(); uilist_refresh(); } else { statusline_readonly(); } break; case 'L': action_list_locate(); break; case 'l': action_list_launch(); break; case 0x0B: /* control-k (up) */ case '[': action_list_move_item_up(); break; case 0x0A: /* control-j (down) */ case ']': action_list_move_item_down(); break; #ifdef DEBUG case '$': debug_i++; snprintf(msg, 80, "Name %d", debug_i); folder_add(current_pw_sublist, msg, "myhost", "myuser", "mypasswd", "mylaucnh"); uilist_refresh(); break; #endif default: break; } } return 0; }
int filter_before_apply(FilterReplyResultBase * const rb, const unsigned int nongeneric_items, const char * const function, const struct sockaddr_storage * const sa_local, const socklen_t sa_local_len, const struct sockaddr_storage * const sa_remote, const socklen_t sa_remote_len) { Filter * const filter = filter_get(); const AppContext * const context = sixjack_get_context(); assert(filter->msgpack_packer != NULL); msgpack_packer * const msgpack_packer = filter->msgpack_packer; msgpack_packer_init(msgpack_packer, filter->msgpack_sbuffer, msgpack_sbuffer_write); unsigned int items_count = nongeneric_items + 5U; if (rb->pre == false) { items_count += 2U; } if (sa_local != NULL) { items_count += 2U; } if (sa_remote != NULL) { items_count += 2U; } msgpack_pack_map(msgpack_packer, items_count); msgpack_pack_mstring(msgpack_packer, "version"); msgpack_pack_unsigned_short(msgpack_packer, VERSION_MAJOR); msgpack_pack_mstring(msgpack_packer, "filter_type"); msgpack_pack_cstring(msgpack_packer, rb->pre ? "PRE" : "POST"); msgpack_pack_mstring(msgpack_packer, "pid"); msgpack_pack_unsigned_int(msgpack_packer, context->pid); msgpack_pack_mstring(msgpack_packer, "function"); msgpack_pack_cstring(msgpack_packer, function); msgpack_pack_mstring(msgpack_packer, "fd"); msgpack_pack_int(msgpack_packer, rb->fd); if (rb->pre == false) { msgpack_pack_mstring(msgpack_packer, "return_value"); msgpack_pack_int(msgpack_packer, *rb->ret); msgpack_pack_mstring(msgpack_packer, "errno"); msgpack_pack_int(msgpack_packer, *rb->ret_errno); } char host[NI_MAXHOST]; in_port_t port; if (sa_local != NULL) { get_name_info(sa_local, sa_local_len, host, &port); msgpack_pack_mstring(msgpack_packer, "local_host"); msgpack_pack_cstring_or_nil(msgpack_packer, host); msgpack_pack_mstring(msgpack_packer, "local_port"); msgpack_pack_unsigned_short(msgpack_packer, (unsigned short) port); } if (sa_remote != NULL) { get_name_info(sa_remote, sa_remote_len, host, &port); msgpack_pack_mstring(msgpack_packer, "remote_host"); msgpack_pack_cstring_or_nil(msgpack_packer, host); msgpack_pack_mstring(msgpack_packer, "remote_port"); msgpack_pack_unsigned_short(msgpack_packer, (unsigned short) port); } return 0; }
PANEL * filter_create() { PANEL *panel; WINDOW *win; int height, width; filter_info_t *info; const char *method; // Calculate window dimensions height = 16; width = 50; // Cerate a new indow for the panel and form win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2); // Create a new panel panel = new_panel(win); // Initialize Filter panel specific data info = sng_malloc(sizeof(filter_info_t)); // Store it into panel userptr set_panel_userptr(panel, (void*) info); // Initialize the fields info->fields[FLD_FILTER_SIPFROM] = new_field(1, 28, 3, 18, 0, 0); info->fields[FLD_FILTER_SIPTO] = new_field(1, 28, 4, 18, 0, 0); info->fields[FLD_FILTER_SRC] = new_field(1, 18, 5, 18, 0, 0); info->fields[FLD_FILTER_DST] = new_field(1, 18, 6, 18, 0, 0); info->fields[FLD_FILTER_PAYLOAD] = new_field(1, 28, 7, 18, 0, 0); info->fields[FLD_FILTER_REGISTER] = new_field(1, 1, 9, 15, 0, 0); info->fields[FLD_FILTER_INVITE] = new_field(1, 1, 10, 15, 0, 0); info->fields[FLD_FILTER_SUBSCRIBE] = new_field(1, 1, 11, 15, 0, 0); info->fields[FLD_FILTER_NOTIFY] = new_field(1, 1, 12, 15, 0, 0); info->fields[FLD_FILTER_OPTIONS] = new_field(1, 1, 9, 37, 0, 0); info->fields[FLD_FILTER_PUBLISH] = new_field(1, 1, 10, 37, 0, 0); info->fields[FLD_FILTER_MESSAGE] = new_field(1, 1, 11, 37, 0, 0); info->fields[FLD_FILTER_FILTER] = new_field(1, 10, height - 2, 11, 0, 0); info->fields[FLD_FILTER_CANCEL] = new_field(1, 10, height - 2, 30, 0, 0); info->fields[FLD_FILTER_COUNT] = NULL; // Set fields options field_opts_off(info->fields[FLD_FILTER_SIPFROM], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_SIPTO], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_SRC], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_DST], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_PAYLOAD], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_REGISTER], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_INVITE], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_SUBSCRIBE], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_NOTIFY], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_OPTIONS], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_PUBLISH], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_MESSAGE], O_AUTOSKIP); field_opts_off(info->fields[FLD_FILTER_FILTER], O_EDIT); field_opts_off(info->fields[FLD_FILTER_CANCEL], O_EDIT); // Change background of input fields set_field_back(info->fields[FLD_FILTER_SIPFROM], A_UNDERLINE); set_field_back(info->fields[FLD_FILTER_SIPTO], A_UNDERLINE); set_field_back(info->fields[FLD_FILTER_SRC], A_UNDERLINE); set_field_back(info->fields[FLD_FILTER_DST], A_UNDERLINE); set_field_back(info->fields[FLD_FILTER_PAYLOAD], A_UNDERLINE); // Create the form and post it info->form = new_form(info->fields); set_form_sub(info->form, win); post_form(info->form); // Fields labels mvwprintw(win, 3, 3, "SIP From:"); mvwprintw(win, 4, 3, "SIP To:"); mvwprintw(win, 5, 3, "Source:"); mvwprintw(win, 6, 3, "Destination:"); mvwprintw(win, 7, 3, "Payload:"); mvwprintw(win, 9, 3, "REGISTER [ ]"); mvwprintw(win, 10, 3, "INVITE [ ]"); mvwprintw(win, 11, 3, "SUBSCRIBE [ ]"); mvwprintw(win, 12, 3, "NOTIFY [ ]"); mvwprintw(win, 9, 25, "OPTIONS [ ]"); mvwprintw(win, 10, 25, "PUBLISH [ ]"); mvwprintw(win, 11, 25, "MESSAGE [ ]"); // Get Method filter if (!(method = filter_get(FILTER_METHOD))) method = setting_get_value(SETTING_FILTER_METHODS); // Set Default field values set_field_buffer(info->fields[FLD_FILTER_SIPFROM], 0, filter_get(FILTER_SIPFROM)); set_field_buffer(info->fields[FLD_FILTER_SIPTO], 0, filter_get(FILTER_SIPTO)); set_field_buffer(info->fields[FLD_FILTER_SRC], 0, filter_get(FILTER_SOURCE)); set_field_buffer(info->fields[FLD_FILTER_DST], 0, filter_get(FILTER_DESTINATION)); set_field_buffer(info->fields[FLD_FILTER_PAYLOAD], 0, filter_get(FILTER_PAYLOAD)); set_field_buffer(info->fields[FLD_FILTER_REGISTER], 0, strcasestr(method, sip_method_str(SIP_METHOD_REGISTER)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_INVITE], 0, strcasestr(method, sip_method_str(SIP_METHOD_INVITE)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_SUBSCRIBE], 0, strcasestr(method,sip_method_str(SIP_METHOD_SUBSCRIBE)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_NOTIFY], 0, strcasestr(method, sip_method_str(SIP_METHOD_NOTIFY)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_OPTIONS], 0, strcasestr(method, sip_method_str(SIP_METHOD_OPTIONS)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_PUBLISH], 0, strcasestr(method, sip_method_str(SIP_METHOD_PUBLISH)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_MESSAGE], 0, strcasestr(method, sip_method_str(SIP_METHOD_MESSAGE)) ? "*" : ""); set_field_buffer(info->fields[FLD_FILTER_FILTER], 0, "[ Filter ]"); set_field_buffer(info->fields[FLD_FILTER_CANCEL], 0, "[ Cancel ]"); // Set the window title and boxes mvwprintw(win, 1, 18, "Filter options"); wattron(win, COLOR_PAIR(CP_BLUE_ON_DEF)); title_foot_box(panel); mvwhline(win, 8, 1, ACS_HLINE, 49); mvwaddch(win, 8, 0, ACS_LTEE); mvwaddch(win, 8, 49, ACS_RTEE); wattroff(win, COLOR_PAIR(CP_BLUE_ON_DEF)); // Set default cursor position set_current_field(info->form, info->fields[FLD_FILTER_SIPFROM]); wmove(win, 3, 18); curs_set(1); return panel; }