static void add_push_application_id(Octstr *appid_flag, Octstr **content, int use_string) { if (use_string) { *content = octstr_format("%S\r\n", appid_string); return; } if (octstr_compare(appid_flag, octstr_imm("any")) == 0) { if (!use_numeric) *content = octstr_create("X-WAP-Application-Id: http://www.wiral.com:*\r\n"); else *content = octstr_create("X-WAP-Application-Id: 0\r\n"); } else if (octstr_compare(appid_flag, octstr_imm("ua")) == 0) { if (!use_numeric) *content = octstr_create("X-WAP-Application-Id: http://www.wiral.com:wml.ua\r\n"); else *content = octstr_create("X-WAP-Application-Id: 2\r\n"); } else if (octstr_compare(appid_flag, octstr_imm("mms")) == 0) { if (!use_numeric) *content = octstr_create("X-WAP-Application-Id: mms.ua\r\n"); else *content = octstr_create("X-WAP-Application-Id: 4\r\n"); } else if (octstr_compare(appid_flag, octstr_imm("scrap")) == 0) { if (!use_numeric) *content = octstr_create("X-WAP-Application-Id: no appid at all\r\n"); else *content = octstr_create("X-WAP-Application-Id: this is not a numeric header\r\n"); } }
static int auth_check(Octstr *user, Octstr *pass, List *headers, int *has_auth_hdr) { int i, res = -1; Octstr *v = http_header_value(headers, octstr_imm("Authorization")); Octstr *p = NULL, *q = NULL; *has_auth_hdr = (v != NULL); if (octstr_len(user) == 0) { res = 0; goto done; } if (!v || octstr_search(v, octstr_imm("Basic "), 0) != 0) goto done; p = octstr_copy(v, sizeof "Basic", octstr_len(v)); octstr_base64_to_binary(p); i = octstr_search_char(p, ':', 0); q = octstr_copy(p, i+1, octstr_len(p)); octstr_delete(p, i, octstr_len(p)); /* p = user, q = pass. */ if (octstr_compare(user, p) != 0 || octstr_compare(pass, q) != 0) res = -1; else res = 0; done: octstr_destroy(v); octstr_destroy(p); octstr_destroy(q); return res; }
static void add_push_application_id(List **push_headers, Octstr *appid_flag, int use_string) { if (use_string) { gwlist_append(*push_headers, appid_string); return; } if (octstr_compare(appid_flag, octstr_imm("any")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "http://www.wiral.com:*"); else http_header_add(*push_headers, "X-WAP-Application-Id", "0"); } else if (octstr_compare(appid_flag, octstr_imm("ua")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "http://www.wiral.com:wml.ua"); else http_header_add(*push_headers, "X-WAP-Application-Id", "2"); } else if (octstr_compare(appid_flag, octstr_imm("mms")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "mms.ua"); else http_header_add(*push_headers, "X-WAP-Application-Id", "4"); } else if (octstr_compare(appid_flag, octstr_imm("scrap")) == 0) { if (!use_numeric) http_header_add(*push_headers, "X-WAP-Application-Id", "no appid at all"); else http_header_add(*push_headers, "X-WAP-Application-Id", "this is not a numeric header"); } }
/* * check if the password matches. Return NULL if * it does (or is not required) */ static Octstr *httpd_check_authorization(List *cgivars, int status) { Octstr *password; static double sleep = 0.01; password = http_cgi_variable(cgivars, "password"); if (status) { if (ha_status_pw == NULL) return NULL; if (password == NULL) goto denied; if (octstr_compare(password, ha_password)!=0 && octstr_compare(password, ha_status_pw)!=0) goto denied; } else { if (password == NULL || octstr_compare(password, ha_password)!=0) goto denied; } sleep = 0.0; return NULL; /* allowed */ denied: gwthread_sleep(sleep); sleep += 1.0; /* little protection against brute force * password cracking */ return octstr_create("Denied"); }
static void add_connection_header(List **push_headers, Octstr *connection) { if (!connection) return; if (octstr_compare(connection, octstr_imm("close")) == 0) http_header_add(*push_headers, "Connection", "close"); else if (octstr_compare(connection, octstr_imm("keep-alive")) == 0) http_header_add(*push_headers, "Connection", "keep-alive"); }
/* * Initialize specifically dlr storage. If defined storage is unknown * then panic. */ void dlr_init(Cfg* cfg) { CfgGroup *grp; Octstr *dlr_type; /* check which DLR storage type we are using */ grp = cfg_get_single_group(cfg, octstr_imm("core")); if(grp == NULL) panic(0, "DLR: can't find group core"); dlr_type = cfg_get(grp, octstr_imm("dlr-storage")); /* * assume we are using internal memory in case no directive * has been specified, warn the user anyway */ if (dlr_type == NULL) { dlr_type = octstr_imm("internal"); warning(0, "DLR: using default 'internal' for storage type."); } /* call the sub-init routine */ if (octstr_compare(dlr_type, octstr_imm("mysql")) == 0) { handles = dlr_init_mysql(cfg); } else if (octstr_compare(dlr_type, octstr_imm("sdb")) == 0) { handles = dlr_init_sdb(cfg); } else if (octstr_compare(dlr_type, octstr_imm("oracle")) == 0) { handles = dlr_init_oracle(cfg); } else if (octstr_compare(dlr_type, octstr_imm("internal")) == 0) { handles = dlr_init_mem(cfg); } else if (octstr_compare(dlr_type, octstr_imm("pgsql")) == 0) { handles = dlr_init_pgsql(cfg); } /* * add aditional types here */ if (handles == NULL) { panic(0, "DLR: storage type '%s' is not supported!", octstr_get_cstr(dlr_type)); } /* check needed function pointers */ if (handles->dlr_add == NULL || handles->dlr_get == NULL || handles->dlr_remove == NULL) panic(0, "DLR: storage type '%s' don't implement needed functions", octstr_get_cstr(dlr_type)); /* get info from storage */ info(0, "DLR using storage type: %s", handles->type); /* cleanup */ octstr_destroy(dlr_type); }
static int have_cookie(List *cookies, Cookie *cookie) { Cookie *value = NULL; long pos = 0; if (cookies == NULL || cookie == NULL) { error(0, "have_cookie: Null argument(s) - no Cookie list, Cookie or both"); return 0; } /* Walk through the cookie cache, comparing cookie */ while (pos < gwlist_len(cookies)) { value = gwlist_get(cookies, pos); /* octstr_compare() now only returns 0 on an exact match or if both args are 0 */ debug ("wap.wsp.http", 0, "have_cookie: Comparing name (%s:%s), path (%s:%s), domain (%s:%s)", octstr_get_cstr(cookie->name), octstr_get_cstr(value->name), octstr_get_cstr(cookie->path), octstr_get_cstr(value->path), octstr_get_cstr(cookie->domain), octstr_get_cstr(value->domain)); /* Match on no value or value and value equality for name, path and domain */ if ( (value->name == NULL || ((value->name != NULL && cookie->name != NULL) && octstr_compare(value->name, cookie->name) == 0)) && (value->path == NULL || ((value->path != NULL && cookie->path != NULL) && octstr_compare(value->path, cookie->path) == 0)) && (value->domain == NULL || ((value->domain != NULL && cookie->domain != NULL) && octstr_compare(value->domain, cookie->domain) == 0)) ) { /* We have a match according to 4.3.3 - discard the old one */ cookie_destroy(value); gwlist_delete(cookies, pos, 1); /* Discard the new cookie also if max-age is 0 - set if expiry date is up */ if (cookie->max_age == 0) { debug("wap.wsp.http", 0, "have_cookie: Discarding expired cookie (%s)", octstr_get_cstr(cookie->name)); return 1; } debug("wap.wsp.http", 0, "have_cookie: Updating cached cookie (%s)", octstr_get_cstr (cookie->name)); break; } else { pos++; } } return 0; }
/* * Returns bearerbox ip address. Resolve it, if the address is localhost. Do * not panic here. Even if we cannot do push, we still can do pull. */ static Octstr *name(Octstr *in) { if (octstr_compare(in, octstr_imm("localhost")) != 0) return octstr_duplicate(in); else return octstr_duplicate(get_official_ip()); }
/* * Input: List of headers containing only X-Wap-Application-Id headers, values * being numeric application id codes. (Ppg module does coding of the header * value part of the X-WAP-Application-Id header). * Output: Octstr containing them in a byte list (one id per byte). * * Returns: Octstr containing headers, if succesfull, otherwise an empty * octstr. */ static Octstr *pack_appid_list(List *headers) { Octstr *appid_os, *header_name, *header_value; long i; size_t len; i = 0; appid_os = octstr_create(""); len = (size_t) list_len(headers); gw_assert(len); while (i < len) { http_header_get(headers, i, &header_name, &header_value); gw_assert(octstr_compare(header_name, octstr_imm("X-WAP-Application-Id")) == 0); octstr_format_append(appid_os, "%S", header_value); octstr_destroy(header_name); octstr_destroy(header_value); ++i; } return appid_os; }
static int my_sort_cmp(const void *a, const void *b) { const Octstr *fa = a; const Octstr *fb = b; return octstr_compare(fa, fb); }
static int cid_matches(List *headers, Octstr *start) { Octstr *cid = http_header_value(headers, octstr_imm("Content-ID")); char *cid_str; int cid_len; int ret; if (cid == NULL) return 0; /* First, strip the <> if any. XXX some mime coders produce such messiness! */ cid_str = octstr_get_cstr(cid); cid_len = octstr_len(cid); if (cid_str[0] == '<') { cid_str+=1; cid_len-=2; } if (start != NULL && cid != NULL && (octstr_compare(start, cid) == 0 || octstr_str_ncompare(start, cid_str, cid_len) == 0)) ret = 1; else ret = 0; octstr_destroy(cid); return ret; }
static void add_content_type(Octstr *content_flag, Octstr **content) { if (*content == NULL) *content = octstr_create(""); if (octstr_compare(content_flag, octstr_imm("wml")) == 0) octstr_append_cstr(*content, "Content-Type: text/vnd.wap.wml\r\n"); else if (octstr_compare(content_flag, octstr_imm("si")) == 0) octstr_append_cstr(*content, "Content-Type: text/vnd.wap.si\r\n"); else if (octstr_compare(content_flag, octstr_imm("sl")) == 0) octstr_append_cstr(*content, "Content-Type: text/vnd.wap.sl\r\n"); else if (octstr_compare(content_flag, octstr_imm("multipart")) == 0) octstr_append_cstr(*content, "Content-Type: multipart/related; boundary=fsahgwruijkfldsa\r\n"); else if (octstr_compare(content_flag, octstr_imm("mms")) == 0) octstr_append_cstr(*content, "Content-Type: application/vnd.wap.mms-message\r\n"); }
int main(int argc, char **argv) { Octstr *os1, *os2; gwlib_init(); os1 = octstr_create(""); octstr_append_from_hex(os1, "411810124550421715161a"); os2 = octstr_duplicate(os1); debug("", 0, "Orginal GSM charset data:"); octstr_dump(os1, 0); charset_gsm_to_utf8(os1); debug("", 0, "Same data mapped to URT-8 charset:"); octstr_dump(os1, 0); charset_utf8_to_gsm(os1); debug("", 0, "Same data mapped back again to GSM charset:"); octstr_dump(os1, 0); if (octstr_compare(os1, os2) != 0) panic(0, "Data is not the same after re-mapping!"); else debug("",0,"Data is same, ok."); octstr_destroy(os1); octstr_destroy(os2); gwlib_shutdown(); return 0; }
/* 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; }
mCfgGrp *mms_get_multi_by_field(mCfg *cfg, Octstr *name, Octstr *field, Octstr *value) { gw_assert(name); gw_assert(is_multigroup(name) == 1); if (!valid_in_group(name, field)) panic(0, "Request for invalid field/variable `%s' in group `%s', unexpected!", octstr_get_cstr(field), octstr_get_cstr(name)); if (cfg->xcfg == NULL) { mCfgGrp *grp; Octstr *val; List *r; int i; r = dict_get(cfg->grps, name); if (r) for (i = 0; i < gwlist_len(r); i++) if ((grp = gwlist_get(r, i)) != NULL && (val = dict_get(grp->fields, field)) != NULL && octstr_compare(val, value) == 0) return grp; return NULL; } else return cfg->cfg_funcs->get_multi_by_field(cfg->xcfg, name, field, value); }
static void add_delimiter(Octstr **content) { if (octstr_compare(delimiter, octstr_imm("crlf")) == 0) { octstr_format_append(*content, "%c", '\r'); } octstr_format_append(*content, "%c", '\n'); }
static Octstr *cgwop_get(struct cgwop *cgwop, Octstr *name) { int len = cgwop->num_fields; while (--len >= 0) if (octstr_compare(name, cgwop->name[len]) == 0) return cgwop->value[len]; return NULL; }
static void transfer_encode (Octstr *cte, Octstr *content) { if (!cte) return; if (octstr_compare(cte, octstr_imm("base64")) == 0) { octstr_binary_to_base64(content); } }
static Octstr *get_string_value_or_return_null(Octstr *str) { if (str == NULL) { return octstr_create("NULL"); } else if (octstr_compare(str, octstr_imm("")) == 0) { return octstr_create("NULL"); } octstr_replace(str, octstr_imm("\\"), octstr_imm("\\\\")); octstr_replace(str, octstr_imm("\'"), octstr_imm("\\\'")); return octstr_format("\'%S\'", str); }
int wap_map_user(Octstr **msisdn, Octstr *user, Octstr *pass) { struct user_map_struct *entry; entry = dict_get(user_map, user); if (entry != NULL && octstr_compare(pass, entry->pass) == 0) { *msisdn = octstr_duplicate(entry->msisdn); return 1; } return 0; }
static int cmp_route(void *ap, void *ms) { AddrPar *addr = ap; Msg *msg = ms; if (msg->wdp_datagram.source_port == addr->port && octstr_compare(msg->wdp_datagram.source_address, addr->address)==0) return 1; return 0; }
static void add_content_transfer_encoding_type(Octstr *content_flag, Octstr *wap_content) { if (!content_flag) return; if (octstr_compare(content_flag, octstr_imm("base64")) == 0) octstr_append_cstr(wap_content, "Content-transfer-encoding: base64"); add_delimiter(&wap_content); }
URLTranslation *urltrans_find_username(URLTranslationList *trans, Octstr *name) { URLTranslation *t; int i; gw_assert(name != NULL); for (i = 0; i < gwlist_len(trans->list); ++i) { t = gwlist_get(trans->list, i); if (t->type == TRANSTYPE_SENDSMS) { if (octstr_compare(name, t->username) == 0) return t; } } return NULL; }
static void check_reversible(void) { Octstr *dates; long pos, endpos, tabpos; Octstr *date, *canondate; long timeval; dates = octstr_read_file("checks/test_dates"); if (dates == NULL) return; for (pos = 0; ; pos = endpos + 1) { endpos = octstr_search_char(dates, '\n', pos); if (endpos < 0) break; tabpos = octstr_search_char(dates, '\t', pos); if (tabpos >= 0 && tabpos < endpos) { date = octstr_copy(dates, pos, tabpos - pos); canondate = octstr_copy(dates, tabpos + 1, endpos - tabpos - 1); } else { date = octstr_copy(dates, pos, endpos - pos); canondate = octstr_duplicate(date); } timeval = date_parse_http(date); if (timeval == -1) warning(0, "Could not parse date \"%s\"", octstr_get_cstr(date)); else { Octstr *newdate; newdate = date_format_http((unsigned long) timeval); if (octstr_compare(newdate, canondate) != 0) { warning(0, "Date not reversible: \"%s\" becomes \"%s\"", octstr_get_cstr(date), octstr_get_cstr(newdate)); } octstr_destroy(newdate); } octstr_destroy(date); octstr_destroy(canondate); } octstr_destroy(dates); }
int radius_authenticate_pdu(RADIUS_PDU *pdu, Octstr **data, Octstr *secret) { int rc = 0; Octstr *stream; Octstr *attributes; Octstr *digest; stream = attributes = digest = NULL; /* first extract attributes from raw data, where * the first 20 octets are code, idendifier, length * and authenticator value as described in RFC2866, sec. 3 */ if (octstr_len(*data) > 20) attributes = octstr_copy(*data, 20, octstr_len(*data)-20); switch (pdu->type) { case 0x04: /* Accounting-Request, see RFC2866, page 6 */ stream = octstr_copy(*data, 0, 4); octstr_append_data(stream, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16); octstr_append(stream, attributes); octstr_append(stream, secret); digest = md5(stream); rc = octstr_compare(pdu->u.Accounting_Request.authenticator, digest) == 0 ? 1 : 0; break; case 0x05: /* Accounting-Response, create Response authenticator */ stream = octstr_duplicate(*data); octstr_append(stream, secret); digest = md5(stream); octstr_delete(*data, 4, 16); octstr_insert(*data, digest, 4); break; default: break; } octstr_destroy(attributes); octstr_destroy(stream); octstr_destroy(digest); return rc; }
static int wsp_cap_get_data(List *caps_list, int id, Octstr *name, Octstr **data) { long i, len; Capability *cap; int found; len = gwlist_len(caps_list); found = 0; *data = NULL; for (i = 0; i < len; i++) { cap = gwlist_get(caps_list, i); if ((name && cap->name && octstr_compare(name, cap->name) == 0) || (!name && cap->id == id)) { if (!found) *data = cap->data; found++; } } return found; }
static void check_comparisons(void) { static const char *tab[] = { "", "a", "ab", "abc", "abcåäö", "ABCÅÄÖ", }; static const int n = sizeof(tab) / sizeof(tab[0]); int i, j; int sign_str, sign_oct; Octstr *os1, *os2; for (i = 0; i < n; ++i) { os1 = octstr_create(tab[i]); for (j = 0; j < n; ++j) { os2 = octstr_create(tab[j]); sign_str = signof(strcmp(tab[i], tab[j])); sign_oct = signof(octstr_compare(os1, os2)); if (sign_str != sign_oct) panic(0, "strcmp (%d) and octstr_compare (%d) differ for " "`%s' and `%s'", sign_str, sign_oct, tab[i], tab[j]); sign_str = signof(strcasecmp(tab[i], tab[j])); sign_oct = signof(octstr_case_compare(os1, os2)); if (sign_str != sign_oct) panic(0, "strcasecmp (%d) and octstr_case_compare (%d) " "differ for `%s' and `%s'", sign_str, sign_oct, tab[i], tab[j]); octstr_destroy(os2); } octstr_destroy(os1); } }
/* * this function receives an WDP message and adds it to * corresponding outgoing_list. */ int udp_addwdp(Msg *msg) { int i; Udpc *udpc, *def_udpc; Octstr *ip; def_udpc = NULL; if (!udp_running) return -1; assert(msg != NULL); assert(msg_type(msg) == wdp_datagram); gwlist_lock(udpc_list); /* select in which list to add this */ for (i=0; i < gwlist_len(udpc_list); i++) { udpc = gwlist_get(udpc_list, i); if (msg->wdp_datagram.source_port == udp_get_port(udpc->addr)) { def_udpc = udpc; ip = udp_get_ip(udpc->addr); if (octstr_compare(msg->wdp_datagram.source_address, ip) == 0) { octstr_destroy(ip); gwlist_produce(udpc->outgoing_list, msg); gwlist_unlock(udpc_list); return 0; } octstr_destroy(ip); } } if (NULL != def_udpc) { gwlist_produce(def_udpc->outgoing_list, msg); gwlist_unlock(udpc_list); return 0; } gwlist_unlock(udpc_list); return -1; }
static Udpc *udpc_find_mapping(Msg *msg, int inbound) { int i; Udpc *udpc; Octstr *addr; /* check if there is allready a bound UDP port */ gwlist_lock(udpc_list); for (i=0; i < gwlist_len(udpc_list); i++) { udpc = gwlist_get(udpc_list, i); /* decide if we compare against inbound or outbound traffic mapping */ addr = inbound ? udpc->map_addr : udpc->addr; if (msg->wdp_datagram.source_port == udp_get_port(addr) && octstr_compare(msg->wdp_datagram.source_address, udp_get_ip(addr)) == 0) { gwlist_unlock(udpc_list); return udpc; } } gwlist_unlock(udpc_list); return NULL; }
static void add_content_type(Octstr *content_flag, Octstr **wap_content) { if (octstr_compare(content_flag, octstr_imm("wml")) == 0) *wap_content = octstr_format("%s", "Content-Type: text/vnd.wap.wml"); else if (octstr_compare(content_flag, octstr_imm("si")) == 0) *wap_content = octstr_format("%s", "Content-Type: text/vnd.wap.si"); else if (octstr_compare(content_flag, octstr_imm("sl")) == 0) *wap_content = octstr_format("%s", "Content-Type: text/vnd.wap.sl"); else if (octstr_compare(content_flag, octstr_imm("multipart")) == 0) *wap_content = octstr_format("%s", "Content-Type: multipart/related; boundary=fsahgwruijkfldsa"); else if (octstr_compare(content_flag, octstr_imm("mms")) == 0) *wap_content = octstr_format("%s", "Content-Type: application/vnd.wap.mms-message"); else if (octstr_compare(content_flag, octstr_imm("scrap")) == 0) *wap_content = octstr_format("%s", "no type at all"); else if (octstr_compare(content_flag, octstr_imm("nil")) == 0) *wap_content = octstr_create(""); if (octstr_len(*wap_content) > 0) add_delimiter(wap_content); }