📄 urltrans.c
字号:
if (aliases == NULL) ot->aliases = list_create(); else { long i; Octstr *os; ot->aliases = octstr_split(aliases, octstr_imm(";")); octstr_destroy(aliases); for (i = 0; i < list_len(ot->aliases); ++i) { os = list_get(ot->aliases, i); octstr_convert_range(os, 0, octstr_len(os), tolower); } } accepted_smsc = cfg_get(grp, octstr_imm("accepted-smsc")); if (accepted_smsc != NULL) { ot->accepted_smsc = octstr_split(accepted_smsc, octstr_imm(";")); octstr_destroy(accepted_smsc); } accepted_smsc_regex = cfg_get(grp, octstr_imm("accepted-smsc-regex")); if (accepted_smsc_regex != NULL) { if ( (ot->accepted_smsc_regex = gw_regex_comp(accepted_smsc_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'", octstr_get_cstr(accepted_smsc_regex)); octstr_destroy(accepted_smsc_regex); } cfg_get_bool(&ot->assume_plain_text, grp, octstr_imm("assume-plain-text")); cfg_get_bool(&ot->accept_x_kannel_headers, grp, octstr_imm("accept-x-kannel-headers")); cfg_get_bool(&ot->strip_keyword, grp, octstr_imm("strip-keyword")); cfg_get_bool(&ot->send_sender, grp, octstr_imm("send-sender")); ot->prefix = cfg_get(grp, octstr_imm("prefix")); ot->suffix = cfg_get(grp, octstr_imm("suffix")); ot->allowed_recv_prefix = cfg_get(grp, octstr_imm("allowed-receiver-prefix")); allowed_receiver_prefix_regex = cfg_get(grp, octstr_imm("allowed-receiver-prefix-regex")); if (allowed_receiver_prefix_regex != NULL) { if ((ot->allowed_receiver_prefix_regex = gw_regex_comp(allowed_receiver_prefix_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_receiver_prefix_regex)); octstr_destroy(allowed_receiver_prefix_regex); } ot->allowed_recv_prefix = cfg_get(grp, octstr_imm("allowed-receiver-prefix")); ot->denied_recv_prefix = cfg_get(grp, octstr_imm("denied-receiver-prefix")); denied_receiver_prefix_regex = cfg_get(grp, octstr_imm("denied-receiver-prefix-regex")); if (denied_receiver_prefix_regex != NULL) { if ((ot->denied_receiver_prefix_regex = gw_regex_comp(denied_receiver_prefix_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'",octstr_get_cstr(denied_receiver_prefix_regex)); octstr_destroy(denied_receiver_prefix_regex); } ot->args = count_occurences(ot->pattern, octstr_imm("%s")); ot->args += count_occurences(ot->pattern, octstr_imm("%S")); ot->has_catchall_arg = (count_occurences(ot->pattern, octstr_imm("%r")) > 0) || (count_occurences(ot->pattern, octstr_imm("%a")) > 0); } else { ot->type = TRANSTYPE_SENDSMS; ot->pattern = octstr_create(""); ot->args = 0; ot->has_catchall_arg = 0; ot->catch_all = 1; ot->username = cfg_get(grp, octstr_imm("username")); ot->password = cfg_get(grp, octstr_imm("password")); ot->dlr_url = cfg_get(grp, octstr_imm("dlr-url")); if (ot->password == NULL) { error(0, "Password required for send-sms user"); goto error; } ot->name = cfg_get(grp, octstr_imm("name")); if (ot->name == NULL) ot->name = octstr_duplicate(ot->username); forced_smsc = cfg_get(grp, octstr_imm("forced-smsc")); default_smsc = cfg_get(grp, octstr_imm("default-smsc")); if (forced_smsc != NULL) { if (default_smsc != NULL) { info(0, "Redundant default-smsc for send-sms user %s", octstr_get_cstr(ot->username)); } ot->forced_smsc = forced_smsc; octstr_destroy(default_smsc); } else if (default_smsc != NULL) ot->default_smsc = default_smsc; ot->deny_ip = cfg_get(grp, octstr_imm("user-deny-ip")); ot->allow_ip = cfg_get(grp, octstr_imm("user-allow-ip")); ot->default_sender = cfg_get(grp, octstr_imm("default-sender")); } ot->allowed_prefix = cfg_get(grp, octstr_imm("allowed-prefix")); allowed_prefix_regex = cfg_get(grp, octstr_imm("allowed-prefix-regex")); if (allowed_prefix_regex != NULL) { if ((ot->allowed_prefix_regex = gw_regex_comp(allowed_prefix_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'", octstr_get_cstr(allowed_prefix_regex)); octstr_destroy(allowed_prefix_regex); } ot->denied_prefix = cfg_get(grp, octstr_imm("denied-prefix")); denied_prefix_regex = cfg_get(grp, octstr_imm("denied-prefix-regex")); if (denied_prefix_regex != NULL) { if ((ot->denied_prefix_regex = gw_regex_comp(denied_prefix_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'", octstr_get_cstr(denied_prefix_regex)); octstr_destroy(denied_prefix_regex); } os = cfg_get(grp, octstr_imm("white-list")); if (os != NULL) { ot->white_list = numhash_create(octstr_get_cstr(os)); octstr_destroy(os); } white_list_regex = cfg_get(grp, octstr_imm("white-list-regex")); if (white_list_regex != NULL) { if ((ot->white_list_regex = gw_regex_comp(white_list_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'", octstr_get_cstr(white_list_regex)); octstr_destroy(white_list_regex); } os = cfg_get(grp, octstr_imm("black-list")); if (os != NULL) { ot->black_list = numhash_create(octstr_get_cstr(os)); octstr_destroy(os); } black_list_regex = cfg_get(grp, octstr_imm("black-list-regex")); if (black_list_regex != NULL) { if ((ot->black_list_regex = gw_regex_comp(black_list_regex, REG_EXTENDED)) == NULL) panic(0, "Could not compile pattern '%s'", octstr_get_cstr(black_list_regex)); octstr_destroy(black_list_regex); } if (cfg_get_integer(&ot->max_messages, grp, octstr_imm("max-messages")) == -1) ot->max_messages = 1; cfg_get_bool(&ot->concatenation, grp, octstr_imm("concatenation")); cfg_get_bool(&ot->omit_empty, grp, octstr_imm("omit-empty")); ot->header = cfg_get(grp, octstr_imm("header")); ot->footer = cfg_get(grp, octstr_imm("footer")); ot->faked_sender = cfg_get(grp, octstr_imm("faked-sender")); ot->split_chars = cfg_get(grp, octstr_imm("split-chars")); ot->split_suffix = cfg_get(grp, octstr_imm("split-suffix")); if ( (ot->prefix == NULL && ot->suffix != NULL) || (ot->prefix != NULL && ot->suffix == NULL) ) { warning(0, "Service <%s>: suffix and prefix are only used" " if both are set.", octstr_get_cstr(ot->keyword)); } if ((ot->prefix != NULL || ot->suffix != NULL) && ot->type != TRANSTYPE_GET_URL) { warning(0, "Service <%s>: suffix and prefix are only used" " if type is 'get-url'.", octstr_get_cstr(ot->keyword)); } return ot;error: error(0, "Couldn't create a URLTranslation."); destroy_onetrans(ot); return NULL;}/* * Free one URLTranslation. */static void destroy_onetrans(void *p) { URLTranslation *ot; ot = p; if (ot != NULL) { octstr_destroy(ot->keyword); list_destroy(ot->aliases, octstr_destroy_item); octstr_destroy(ot->dlr_url); octstr_destroy(ot->pattern); octstr_destroy(ot->prefix); octstr_destroy(ot->suffix); octstr_destroy(ot->default_sender); octstr_destroy(ot->faked_sender); octstr_destroy(ot->split_chars); octstr_destroy(ot->split_suffix); octstr_destroy(ot->header); octstr_destroy(ot->footer); list_destroy(ot->accepted_smsc, octstr_destroy_item); octstr_destroy(ot->name); octstr_destroy(ot->username); octstr_destroy(ot->password); octstr_destroy(ot->forced_smsc); octstr_destroy(ot->default_smsc); octstr_destroy(ot->allow_ip); octstr_destroy(ot->deny_ip); octstr_destroy(ot->allowed_prefix); octstr_destroy(ot->denied_prefix); octstr_destroy(ot->allowed_recv_prefix); octstr_destroy(ot->denied_recv_prefix); numhash_destroy(ot->white_list); numhash_destroy(ot->black_list); if (ot->keyword_regex != NULL) gw_regex_destroy(ot->keyword_regex); if (ot->accepted_smsc_regex != NULL) gw_regex_destroy(ot->accepted_smsc_regex); if (ot->allowed_prefix_regex != NULL) gw_regex_destroy(ot->allowed_prefix_regex); if (ot->denied_prefix_regex != NULL) gw_regex_destroy(ot->denied_prefix_regex); if (ot->allowed_receiver_prefix_regex != NULL) gw_regex_destroy(ot->allowed_receiver_prefix_regex); if (ot->denied_receiver_prefix_regex != NULL) gw_regex_destroy(ot->denied_receiver_prefix_regex); if (ot->white_list_regex != NULL) gw_regex_destroy(ot->white_list_regex); if (ot->black_list_regex != NULL) gw_regex_destroy(ot->black_list_regex); gw_free(ot); }}/* * checks if the number of passed words matches the service-pattern defined in the * translation. returns 0 if arguments are okay, -1 otherwise. */static int check_num_args(URLTranslation *t, List *words){ const int IS_OKAY = 0; const int NOT_OKAY = -1; int n; n = list_len(words); /* check number of arguments */ if (t->catch_all) return IS_OKAY; if (n - 1 == t->args) return IS_OKAY; if (t->has_catchall_arg && n - 1 >= t->args) return IS_OKAY; return NOT_OKAY;}/* * checks if a request matches the parameters of a URL-Translation, e.g. whether or not * a user is allowed to use certain services. returns 0 if allowed, -1 if not. * reject will be set to 1 is a number is rejected due to white/black-lists. */static int check_allowed_translation(URLTranslation *t, Octstr *smsc, Octstr *sender, Octstr *receiver, int *reject){ const int IS_ALLOWED = 0; const int NOT_ALLOWED = -1; /* if smsc_id set and accepted_smsc exist, accept * translation only if smsc id is in accept string */ if (smsc && t->accepted_smsc) { if (!list_search(t->accepted_smsc, smsc, octstr_item_match)) return NOT_ALLOWED; }; if (smsc && t->accepted_smsc_regex) if (gw_regex_matches( t->accepted_smsc_regex, smsc) == NO_MATCH) return NOT_ALLOWED; /* Have allowed for sender */ if (t->allowed_prefix && ! t->denied_prefix && (does_prefix_match(t->allowed_prefix, sender) != 1)) return NOT_ALLOWED; if (t->allowed_prefix_regex && ! t->denied_prefix_regex) if (gw_regex_matches( t->allowed_prefix_regex, sender) == NO_MATCH) return NOT_ALLOWED; /* Have denied for sender */ if (t->denied_prefix && ! t->allowed_prefix && (does_prefix_match(t->denied_prefix, sender) == 1)) return NOT_ALLOWED; if (t->denied_prefix_regex && ! t->allowed_prefix_regex) if (gw_regex_matches( t->denied_prefix_regex, sender) == NO_MATCH) return NOT_ALLOWED; /* Have allowed for receiver */ if (t->allowed_recv_prefix && ! t->denied_recv_prefix && (does_prefix_match(t->allowed_recv_prefix, receiver) != 1)) return NOT_ALLOWED; if (t->allowed_receiver_prefix_regex && ! t->denied_receiver_prefix_regex) if (gw_regex_matches( t->allowed_receiver_prefix_regex, receiver) == NO_MATCH) return NOT_ALLOWED; /* Have denied for receiver */ if (t->denied_recv_prefix && ! t->allowed_recv_prefix && (does_prefix_match(t->denied_recv_prefix, receiver) == 1)) return NOT_ALLOWED; if (t->denied_receiver_prefix_regex && ! t->allowed_receiver_prefix_regex) if (gw_regex_matches( t->denied_receiver_prefix_regex, receiver) == NO_MATCH) return NOT_ALLOWED; if (t->white_list && numhash_find_number(t->white_list, sender) < 1) { *reject = 1; return NOT_ALLOWED; } if (t->white_list_regex) if (gw_regex_matches( t->white_list_regex, sender) == NO_MATCH) { *reject = 1; return NOT_ALLOWED; } if (t->black_list && numhash_find_number(t->black_list, sender) == 1) { *reject = 1; return NOT_ALLOWED; } if (t->black_list_regex) if (gw_regex_matches(t->black_list_regex, sender) == MATCH) { *reject = 1; return NOT_ALLOWED; } /* Have allowed and denied */ if (t->denied_prefix && t->allowed_prefix && (does_prefix_match(t->allowed_prefix, sender) != 1) && (does_prefix_match(t->denied_prefix, sender) == 1) ) return NOT_ALLOWED; if (t->denied_prefix_regex && t->allowed_prefix_regex && (gw_regex_matches(t->allowed_prefix_regex, sender) == NO_MATCH) && (gw_regex_matches(t->denied_prefix_regex, sender) == MATCH)) return NOT_ALLOWED; return IS_ALLOWED;}; /* get_matching_translations - iterate over all translations in trans. * for each translation check whether * the translation's keyword has already been interpreted as a regexp. * if not, compile it now, * otherwise retrieve compilation result from dictionary. * * the translations where the word matches the translation's pattern * are returned in a list * */static List* get_matching_translations(URLTranslationList *trans, Octstr *word) { List *list; /*char *tmp_word;*/ int i; size_t n_match = 1; regmatch_t p_match[10]; URLTranslation *t; gw_assert(trans != NULL && word != NULL); list = list_create(); for (i = 0; i < list_len(trans->list); ++i) { t = list_get(trans->list, i); if (t->keyword == NULL) continue; /* if regex feature is used try to match */ if ((t->keyword_regex != NULL) && (gw_regex_exec(t->keyword_regex, word, n_match, p_match, 0) == 0)) list_append(list, t); /* otherwise look for exact match */ if (octstr_compare(t->keyword, word) == 0) list_append(list, t); } return list;}/* * Find the appropriate translation */static URLTranslation *find_translation(URLTranslationList *trans, List *words, Octstr *smsc, Octstr *sender, Octstr *receiver, int *reject){ Octstr *keyword; int i, n; URLTranslation *t; List *list; n = list_len(words); if (n == 0) return NULL; n = 1; keyword = list_get(words, 0); keyword = octstr_duplicate(keyword); octstr_convert_range(keyword, 0, octstr_len(keyword), tolower); list = get_matching_translations(trans, keyword); /* list now contains all translations where the keyword of the sms matches the pattern defined by the tranlsation's keyword */ t = NULL; for (i = 0; i < list_len(list); ++i) { t = list_get(list, i); if (check_allowed_translation(t, smsc, sender, receiver, reject) == 0 && check_num_args(t, words) == 0) break; t = NULL; } /* Only return reject if there's only blacklisted smsc's */ if(t != NULL) *reject = 0; octstr_destroy(keyword); list_destroy(list, NULL); return t;}static URLTranslation *find_default_translation(URLTranslationList *trans, Octstr *smsc, Octstr *sender, Octstr *receiver, int *reject){ URLTranslation *t; int i; List *list; *reject = 0; list = dict_get(trans->dict, octstr_imm("default")); t = NULL; for (i = 0; i < list_len(list); ++i) { t = list_get(list, i); if (check_allowed_translation(t, smsc, sender, receiver, reject) == 0) break; t = NULL; } /* Only return reject if there's only blacklisted smsc's */ if(t != NULL) *reject = 0; return t;}static URLTranslation *find_black_list_translation(URLTranslationList *trans, Octstr *smsc){ URLTranslation *t; int i; List *list; list = dict_get(trans->dict, octstr_imm("black-list")); t = NULL; for (i = 0; i < list_len(list); ++i) { t = list_get(list, i); if (smsc && t->accepted_smsc) { if (!list_search(t->accepted_smsc, smsc, octstr_item_match)) { t = NULL; continue; } } break; } return t;}/* * Count the number of times `pat' occurs in `str'. */static long count_occurences(Octstr *str, Octstr *pat){ long count; long pos; long len; count = 0; pos = 0; len = octstr_len(pat); while ((pos = octstr_search(str, pat, pos)) != -1) { ++count; pos += len; } return count;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -