X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_zoom.cpp;h=d7ca9356ca780b192773419223390be1f180c635;hb=581e7621e9664408aac1f0386f7d823f4906860c;hp=ae29e0d5f0fedc5b804645862b178aec4d8d8e8d;hpb=0ee1fd4b16bd7c5510c756790e8e4f1d2217de31;p=metaproxy-moved-to-github.git diff --git a/src/filter_zoom.cpp b/src/filter_zoom.cpp index ae29e0d..d7ca935 100644 --- a/src/filter_zoom.cpp +++ b/src/filter_zoom.cpp @@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include +#include #include "torus.hpp" #include @@ -74,6 +76,7 @@ namespace metaproxy_1 { std::string urlRecipe; std::string contentConnector; std::string sortStrategy; + std::string rpn2cql_fname; bool use_turbomarc; bool piggyback; CCL_bibset ccl_bibset; @@ -91,10 +94,12 @@ namespace metaproxy_1 { std::string m_frontend_database; SearchablePtr sptr; xsltStylesheetPtr xsp; - std::string content_session_id; + std::string cproxy_host; bool enable_cproxy; bool enable_explain; xmlDoc *explain_doc; + std::string m_proxy; + cql_transform_t cqlt; public: Backend(); ~Backend(); @@ -114,16 +119,21 @@ namespace metaproxy_1 { Impl *m_p; bool m_is_virtual; bool m_in_use; + std::string session_realm; yazpp_1::GDU m_init_gdu; BackendPtr m_backend; void handle_package(mp::Package &package); void handle_search(mp::Package &package); + void auth(mp::Package &package, Z_InitRequest *req, + int *error, char **addinfo, ODR odr); + BackendPtr explain_search(mp::Package &package, std::string &database, int *error, char **addinfo, mp::odr &odr, + std::string torus_url, std::string &torus_db, std::string &realm); void handle_present(mp::Package &package); @@ -169,7 +179,12 @@ namespace metaproxy_1 { ODR odr, BackendPtr b, Odr_oid *preferredRecordSyntax, const char *element_set_name); - + bool retry(mp::Package &package, + mp::odr &odr, + BackendPtr b, + int &error, char **addinfo, + int &proxy_step, int &same_retries, + int &proxy_retries); void log_diagnostic(mp::Package &package, int error, const char *addinfo); public: @@ -186,6 +201,10 @@ namespace metaproxy_1 { const char *path); private: void configure_local_records(const xmlNode * ptr, bool test_only); + bool check_proxy(const char *proxy); + + + FrontendPtr get_frontend(mp::Package &package); void release_frontend(mp::Package &package); SearchablePtr parse_torus_record(const xmlNode *ptr); @@ -195,12 +214,14 @@ namespace metaproxy_1 { boost::condition m_cond_session_ready; std::string torus_searchable_url; std::string torus_content_url; + std::string torus_auth_url; std::string default_realm; std::map fieldmap; std::string xsldir; std::string file_path; std::string content_proxy_server; std::string content_tmp_file; + std::string content_config_file; bool apdu_log; CCL_bibset bibset; std::string element_transform; @@ -210,6 +231,7 @@ namespace metaproxy_1 { xsltStylesheetPtr record_xsp; std::map s_map; std::string zoom_timeout; + int proxy_timeout; }; } } @@ -278,6 +300,7 @@ yf::Zoom::Backend::~Backend() xsltFreeStylesheet(xsp); if (explain_doc) xmlFreeDoc(explain_doc); + cql_transform_close(cqlt); ZOOM_connection_destroy(m_connection); ZOOM_resultset_destroy(m_resultset); } @@ -300,6 +323,21 @@ void yf::Zoom::Backend::get_zoom_error(int *error, char **addinfo, *error = yaz_diag_srw_to_bib1(error0); else if (!strcmp(dset, "Bib-1")) *error = error0; + else if (!strcmp(dset, "ZOOM")) + { + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + if (error0 == ZOOM_ERROR_INIT) + *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR; + else if (error0 == ZOOM_ERROR_DECODE) + { + if (zoom_addinfo) + { + if (strstr(zoom_addinfo, "Authentication") || + strstr(zoom_addinfo, "authentication")) + *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR; + } + } + } else *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; @@ -430,7 +468,7 @@ void yf::Zoom::Impl::release_frontend(mp::Package &package) yf::Zoom::Impl::Impl() : apdu_log(false), element_transform("pz2") , element_raw("raw"), - zoom_timeout("40") + zoom_timeout("40"), proxy_timeout(1) { bibset = ccl_qual_mk(); @@ -564,6 +602,8 @@ yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr) { s->sortStrategy = mp::xml::get_text(ptr); } + else if (!strcmp((const char *) ptr->name, "rpn2cql")) + s->rpn2cql_fname = mp::xml::get_text(ptr); } return s; } @@ -622,7 +662,6 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, std::string explain_xslt_fname; std::string record_xslt_fname; - content_tmp_file = "/tmp/cf.XXXXXX.p"; if (path && *path) { file_path = path; @@ -640,6 +679,8 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, torus_searchable_url = mp::xml::get_text(attr->children); else if (!strcmp((const char *) attr->name, "content_url")) torus_content_url = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "auth_url")) + torus_auth_url = mp::xml::get_text(attr->children); else if (!strcmp((const char *) attr->name, "realm")) default_realm = mp::xml::get_text(attr->children); else if (!strcmp((const char *) attr->name, "xsldir")) @@ -695,9 +736,19 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, for (attr = ptr->properties; attr; attr = attr->next) { if (!strcmp((const char *) attr->name, "server")) + { + yaz_log(YLOG_WARN, + "contentProxy's server attribute is deprecated"); + yaz_log(YLOG_LOG, + "Specify config_file instead. For example:"); + yaz_log(YLOG_LOG, + " content_file=\"/etc/cf-proxy/cproxy.cfg\""); content_proxy_server = mp::xml::get_text(attr->children); + } else if (!strcmp((const char *) attr->name, "tmp_file")) content_tmp_file = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "config_file")) + content_config_file = mp::xml::get_text(attr->children); else throw mp::filter::FilterException( "Bad attribute " + std::string((const char *) @@ -724,6 +775,8 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, { if (!strcmp((const char *) attr->name, "timeout")) zoom_timeout = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "proxy_timeout")) + proxy_timeout = mp::xml::get_int(attr->children, 1); else throw mp::filter::FilterException( "Bad attribute " + std::string((const char *) @@ -820,16 +873,98 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package, { if (b->sptr->contentConnector.length()) { - char *fname = (char *) xmalloc(m_p->content_tmp_file.length() + 8); - strcpy(fname, m_p->content_tmp_file.c_str()); + std::string proxyhostname; + std::string tmp_file; + bool legacy_format = false; + + if (m_p->content_proxy_server.length()) + { + proxyhostname = m_p->content_proxy_server; + legacy_format = true; + } + + if (m_p->content_tmp_file.length()) + tmp_file = m_p->content_tmp_file; + + if (m_p->content_config_file.length()) + { + FILE *inf = fopen(m_p->content_config_file.c_str(), "r"); + if (inf) + { + char buf[1024]; + while (fgets(buf, sizeof(buf)-1, inf)) + { + char *cp; + cp = strchr(buf, '#'); + if (cp) + *cp = '\0'; + cp = strchr(buf, '\n'); + if (cp) + *cp = '\0'; + cp = strchr(buf, ':'); + if (cp) + { + char *cp1 = cp; + while (cp1 != buf && cp1[-1] == ' ') + cp1--; + *cp1 = '\0'; + cp++; + while (*cp == ' ') + cp++; + if (!strcmp(buf, "proxyhostname")) + proxyhostname = cp; + if (!strcmp(buf, "sessiondir") && *cp) + { + if (cp[strlen(cp)-1] == '/') + cp[strlen(cp)-1] = '\0'; + tmp_file = std::string(cp) + std::string("/cf.XXXXXX.p"); + } + } + } + fclose(inf); + } + else + { + package.log("zoom", YLOG_WARN|YLOG_ERRNO, + "unable to open content config %s", + m_p->content_config_file.c_str()); + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + *addinfo = (char *) odr_malloc(odr, 60 + tmp_file.length()); + sprintf(*addinfo, "unable to open content config %s", + m_p->content_config_file.c_str()); + return false; + } + } + + if (proxyhostname.length() == 0) + { + package.log("zoom", YLOG_WARN, "no proxyhostname"); + return true; + } + if (tmp_file.length() == 0) + { + package.log("zoom", YLOG_WARN, "no tmp_file"); + return true; + } + + char *fname = xstrdup(tmp_file.c_str()); char *xx = strstr(fname, "XXXXXX"); if (!xx) { - xx = fname + strlen(fname); - strcat(fname, "XXXXXX"); + package.log("zoom", YLOG_WARN, "bad tmp_file %s", tmp_file.c_str()); + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + *addinfo = (char *) odr_malloc(odr, 60 + tmp_file.length()); + sprintf(*addinfo, "bad format of content tmp_file: %s", + tmp_file.c_str()); + xfree(fname); + return false; } char tmp_char = xx[6]; sprintf(xx, "%06d", ((unsigned) rand()) % 1000000); + if (legacy_format) + b->cproxy_host = std::string(xx) + "." + proxyhostname; + else + b->cproxy_host = proxyhostname + "/" + xx; xx[6] = tmp_char; FILE *file = fopen(fname, "w"); @@ -842,7 +977,6 @@ bool yf::Zoom::Frontend::create_content_session(mp::Package &package, xfree(fname); return false; } - b->content_session_id.assign(xx, 6); mp::wrbuf w; wrbuf_puts(w, "#content_proxy\n"); wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str()); @@ -866,12 +1000,15 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( std::string &database, int *error, char **addinfo, mp::odr &odr, int *proxy_step) { + bool connection_reuse = false; + std::string proxy; + std::list::const_iterator map_it; if (m_backend && !m_backend->enable_explain && m_backend->m_frontend_database == database) { - m_backend->connect("", error, addinfo, odr); - return m_backend; + connection_reuse = true; + proxy = m_backend->m_proxy; } std::string input_args; @@ -887,9 +1024,10 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( std::string authentication; std::string content_authentication; - std::string proxy; std::string content_proxy; - std::string realm = m_p->default_realm; + std::string realm = session_realm; + if (realm.length() == 0) + realm = m_p->default_realm; const char *param_user = 0; const char *param_password = 0; @@ -936,15 +1074,31 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( char **dstr; int dnum = 0; nmem_strsplit(((ODR) odr)->mem, ",", value, &dstr, &dnum); - if (*proxy_step >= dnum) - *proxy_step = 0; + if (connection_reuse) + { + // find the step after our current proxy + int i; + for (i = 0; i < dnum; i++) + if (!strcmp(proxy.c_str(), dstr[i])) + break; + if (i >= dnum - 1) + *proxy_step = 0; + else + *proxy_step = i + 1; + } else { - proxy = dstr[*proxy_step]; - - (*proxy_step)++; - if (*proxy_step == dnum) + // step is known.. Guess our proxy from it + if (*proxy_step >= dnum) *proxy_step = 0; + else + { + proxy = dstr[*proxy_step]; + + (*proxy_step)++; + if (*proxy_step == dnum) + *proxy_step = 0; + } } } else if (!strcmp(name, "cproxysession")) @@ -953,8 +1107,10 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( out_values[no_out_args++] = value; torus_url = m_p->torus_content_url; } - else if (!strcmp(name, "realm")) + else if (!strcmp(name, "realm") && session_realm.length() == 0) realm = value; + else if (!strcmp(name, "torus_url") && session_realm.length() == 0) + torus_url = value; else if (name[0] == 'x' && name[1] == '-') { out_names[no_out_args] = name; @@ -969,7 +1125,16 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( *addinfo = msg; return notfound; } + } + if (proxy.length()) + package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str()); + + if (connection_reuse) + { + m_backend->connect("", error, addinfo, odr); + return m_backend; } + if (param_user) { authentication = std::string(param_user); @@ -984,8 +1149,8 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( } if (torus_db.compare("IR-Explain---1") == 0) - return explain_search(package, database, error, addinfo, odr, torus_db, - realm); + return explain_search(package, database, error, addinfo, odr, torus_url, + torus_db, realm); SearchablePtr sptr; @@ -1001,33 +1166,51 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( realm, m_p->proxy); if (!doc) { - *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST; - *addinfo = odr_strdup(odr, torus_db.c_str()); + *error = YAZ_BIB1_UNSPECIFIED_ERROR; + *addinfo = odr_strdup(odr, "Torus server unavailable or " + "incorrectly configured"); BackendPtr b; return b; } const xmlNode *ptr = xmlDocGetRootElement(doc); - if (ptr) - { // presumably ptr is a records element node - // parse first record in document - for (ptr = ptr->children; ptr; ptr = ptr->next) + if (ptr && ptr->type == XML_ELEMENT_NODE) + { + if (!strcmp((const char *) ptr->name, "record")) { - if (ptr->type == XML_ELEMENT_NODE - && !strcmp((const char *) ptr->name, "record")) + sptr = m_p->parse_torus_record(ptr); + } + else if (!strcmp((const char *) ptr->name, "records")) + { + for (ptr = ptr->children; ptr; ptr = ptr->next) { - if (sptr) + if (ptr->type == XML_ELEMENT_NODE + && !strcmp((const char *) ptr->name, "record")) { - *error = YAZ_BIB1_UNSPECIFIED_ERROR; - *addinfo = (char*) odr_malloc(odr, 40 + database.length()), - sprintf(*addinfo, "multiple records for udb=%s", - database.c_str()); - xmlFreeDoc(doc); - BackendPtr b; - return b; + if (sptr) + { + *error = YAZ_BIB1_UNSPECIFIED_ERROR; + *addinfo = (char*) + odr_malloc(odr, 40 + torus_db.length()); + sprintf(*addinfo, "multiple records for udb=%s", + database.c_str()); + xmlFreeDoc(doc); + BackendPtr b; + return b; + } + sptr = m_p->parse_torus_record(ptr); } - sptr = m_p->parse_torus_record(ptr); } } + else + { + *error = YAZ_BIB1_UNSPECIFIED_ERROR; + *addinfo = (char*) odr_malloc( + odr, 40 + strlen((const char *) ptr->name)); + sprintf(*addinfo, "bad root element for torus: %s", ptr->name); + xmlFreeDoc(doc); + BackendPtr b; + return b; + } } xmlFreeDoc(doc); } @@ -1108,10 +1291,32 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( } } + cql_transform_t cqlt = 0; + if (sptr->rpn2cql_fname.length()) + { + char fullpath[1024]; + char *cp = yaz_filepath_resolve(sptr->rpn2cql_fname.c_str(), + m_p->file_path.c_str(), 0, fullpath); + if (cp) + cqlt = cql_transform_open_fname(fullpath); + } + else + cqlt = cql_transform_create(); + + if (!cqlt) + { + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + *addinfo = odr_strdup(odr, "Missing/invalid cql2rpn file"); + BackendPtr b; + xsltFreeStylesheet(xsp); + return b; + } + m_backend.reset(); BackendPtr b(new Backend); + b->cqlt = cqlt; b->sptr = sptr; b->xsp = xsp; b->m_frontend_database = database; @@ -1135,6 +1340,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( if (proxy.length() == 0) proxy = sptr->cfProxy; + b->m_proxy = proxy; if (sptr->cfAuth.length()) { @@ -1191,9 +1397,6 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( if (proxy.length()) b->set_option("proxy", proxy); } - if (proxy.length()) - package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str()); - std::string url; if (sptr->sru.length()) { @@ -1297,7 +1500,7 @@ void yf::Zoom::Frontend::prepare_elements(BackendPtr b, } Z_Records *yf::Zoom::Frontend::get_explain_records( - Package &package, + mp::Package &package, Odr_int start, Odr_int number_to_present, int *error, @@ -1356,7 +1559,7 @@ Z_Records *yf::Zoom::Frontend::get_explain_records( } -Z_Records *yf::Zoom::Frontend::get_records(Package &package, +Z_Records *yf::Zoom::Frontend::get_records(mp::Package &package, Odr_int start, Odr_int number_to_present, int *error, @@ -1409,26 +1612,12 @@ Z_Records *yf::Zoom::Frontend::get_records(Package &package, const char *xsl_parms[3]; mp::wrbuf cproxy_host; - if (b->enable_cproxy && b->content_session_id.length()) + if (b->enable_cproxy && b->cproxy_host.length()) { - const char *proxy_server_cstr = m_p->content_proxy_server.c_str(); - const char *session_sub = strstr(proxy_server_cstr, "%s"); - - if (session_sub) - { - wrbuf_puts(cproxy_host, "\""); - wrbuf_write(cproxy_host, proxy_server_cstr, - session_sub - proxy_server_cstr); - wrbuf_puts(cproxy_host, b->content_session_id.c_str()); - wrbuf_puts(cproxy_host, session_sub + 2); - wrbuf_puts(cproxy_host, "/\""); - } - else - { - wrbuf_printf(cproxy_host, "\"%s.%s/\"", - b->content_session_id.c_str(), - proxy_server_cstr); - } + wrbuf_puts(cproxy_host, "\""); + wrbuf_puts(cproxy_host, b->cproxy_host.c_str()); + wrbuf_puts(cproxy_host, "/\""); + xsl_parms[0] = "cproxyhost"; xsl_parms[1] = wrbuf_cstr(cproxy_host); xsl_parms[2] = 0; @@ -1670,6 +1859,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package, int *error, char **addinfo, mp::odr &odr, + std::string torus_url, std::string &torus_db, std::string &realm) { @@ -1696,7 +1886,6 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package, else if (query->which == Z_Query_type_104 && query->u.type_104->which == Z_External_CQL) { - std::string torus_url = m_p->torus_searchable_url; std::string torus_query(query->u.type_104->u.cql); xmlDoc *doc = mp::get_searchable(package, torus_url, "", torus_query, @@ -1711,8 +1900,8 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package, if (!doc) { *error = YAZ_BIB1_UNSPECIFIED_ERROR; - *addinfo = odr_strdup(odr, "IR-Explain---1 problem. " - "Could not obtain Torus records for Explain"); + *addinfo = odr_strdup(odr, "Torus server unavailable or " + "incorrectly configured"); } else { @@ -1739,6 +1928,95 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package, } } +static bool wait_conn(COMSTACK cs, int secs) +{ + struct yaz_poll_fd pfd; + + yaz_poll_add(pfd.input_mask, yaz_poll_except); + if (cs->io_pending && CS_WANT_WRITE) + yaz_poll_add(pfd.input_mask, yaz_poll_write); + if (cs->io_pending & CS_WANT_READ) + yaz_poll_add(pfd.input_mask, yaz_poll_read); + + pfd.fd = cs_fileno(cs); + pfd.client_data = 0; + + int ret = yaz_poll(&pfd, 1, secs, 0); + return ret > 0; +} + +bool yf::Zoom::Impl::check_proxy(const char *proxy) +{ + COMSTACK conn = 0; + const char *uri = "http://localhost/"; + void *add; + mp::odr odr; + bool outcome = false; + conn = cs_create_host_proxy(uri, 0, &add, proxy); + + if (!conn) + return false; + + Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1); + gdu->u.HTTP_Request->method = odr_strdup(odr, "GET"); + + if (z_GDU(odr, &gdu, 0, 0)) + { + int len; + char *buf = odr_getbuf(odr, &len, 0); + + int ret = cs_connect(conn, add); + if (ret > 0 || (ret == 0 && wait_conn(conn, 1))) + { + while (1) + { + ret = cs_put(conn, buf, len); + if (ret != 1) + break; + if (!wait_conn(conn, proxy_timeout)) + break; + } + if (ret == 0) + outcome = true; + } + } + cs_close(conn); + return outcome; +} + +bool yf::Zoom::Frontend::retry(mp::Package &package, + mp::odr &odr, + BackendPtr b, + int &error, char **addinfo, + int &proxy_step, int &same_retries, + int &proxy_retries) +{ + if (b && b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str())) + { + log_diagnostic(package, error, *addinfo); + package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str()); + m_backend.reset(); + if (proxy_step) // there is a failover + { + proxy_retries++; + package.log("zoom", YLOG_WARN, "search failed: trying next proxy"); + return true; + } + error = YAZ_BIB1_PROXY_FAILURE; + *addinfo = odr_strdup(odr, b->m_proxy.c_str()); + } + else if (same_retries == 0 && proxy_retries == 0) + { + log_diagnostic(package, error, *addinfo); + same_retries++; + package.log("zoom", YLOG_WARN, "search failed: retry"); + m_backend.reset(); + proxy_step = 0; + return true; + } + return false; +} + void yf::Zoom::Frontend::handle_search(mp::Package &package) { Z_GDU *gdu = package.request().get(); @@ -1755,6 +2033,8 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) return; } int proxy_step = 0; + int same_retries = 0; + int proxy_retries = 0; next_proxy: @@ -1764,11 +2044,11 @@ next_proxy: BackendPtr b = get_backend_from_databases(package, db, &error, &addinfo, odr, &proxy_step); - if (error && proxy_step) + if (error) { - package.log("zoom", YLOG_WARN, - "create backend failed: trying next proxy"); - goto next_proxy; + if (retry(package, odr, b, error, &addinfo, proxy_step, + same_retries, proxy_retries)) + goto next_proxy; } if (error) { @@ -1844,8 +2124,7 @@ next_proxy: cql_parser_destroy(cp); return; } - char ccl_buf[1024]; - r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf)); + r = cql_to_ccl(cn, wrbuf_vp_puts, ccl_wrbuf); if (r) { error = YAZ_BIB1_MALFORMED_QUERY; @@ -1873,8 +2152,6 @@ next_proxy: mp::wrbuf sort_spec_wrbuf; yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf), sort_spec_wrbuf); - wrbuf_puts(ccl_wrbuf, ccl_buf); - yaz_tok_cfg_t tc = yaz_tok_cfg_create(); yaz_tok_parse_t tp = yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf)); @@ -1980,11 +2257,7 @@ next_proxy: } else { - cql_transform_t cqlt = cql_transform_create(); - - status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery); - - cql_transform_close(cqlt); + status = cql_transform_rpn2cql_wrbuf(b->cqlt, wrb, zquery); } if (status == 0) { @@ -2012,12 +2285,11 @@ next_proxy: ZOOM_query_destroy(q); } - if (error && proxy_step) + if (error) { - // reset below prevent reuse in get_backend_from_databases - m_backend.reset(); - package.log("zoom", YLOG_WARN, "search failed: trying next proxy"); - goto next_proxy; + if (retry(package, odr, b, error, &addinfo, proxy_step, + same_retries, proxy_retries)) + goto next_proxy; } const char *element_set_name = 0; @@ -2158,6 +2430,127 @@ void yf::Zoom::Frontend::handle_package(mp::Package &package) } } +std::string escape_cql_term(std::string inp) +{ + std::string res; + size_t l = inp.length(); + size_t i; + for (i = 0; i < l; i++) + { + if (strchr("*?^\"", inp[i])) + res += "\\"; + res += inp[i]; + } + return res; +} + +void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req, + int *error, char **addinfo, ODR odr) +{ + if (m_p->torus_auth_url.length() == 0) + return; + + std::string user; + std::string password; + if (req->idAuthentication) + { + Z_IdAuthentication *auth = req->idAuthentication; + switch (auth->which) + { + case Z_IdAuthentication_open: + if (auth->u.open) + { + const char *cp = strchr(auth->u.open, '/'); + if (cp) + { + user.assign(auth->u.open, cp - auth->u.open); + password.assign(cp + 1); + } + } + break; + case Z_IdAuthentication_idPass: + if (auth->u.idPass->userId) + user.assign(auth->u.idPass->userId); + if (auth->u.idPass->password) + password.assign(auth->u.idPass->password); + break; + } + } + + std::string ip = package.origin().get_address(); + yaz_log(YLOG_LOG, "IP=%s", ip.c_str()); + + std::string torus_query; + int failure_code; + + if (user.length() && password.length()) + { + torus_query = "userName==\"" + escape_cql_term(user) + + "\" and password==\"" + escape_cql_term(password) + "\""; + failure_code = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD; + } + else + { + const char *ip_cstr = ip.c_str(); + const char *cp = strchr(ip_cstr, ':'); + if (cp) + ip_cstr = cp + 1; + + torus_query = "ip encloses/net.ipaddress \""; + torus_query += escape_cql_term(std::string(ip_cstr)); + torus_query += "\""; + failure_code = YAZ_BIB1_INIT_AC_BLOCKED_NETWORK_ADDRESS; + } + + std::string dummy_db; + std::string dummy_realm; + xmlDoc *doc = mp::get_searchable(package, m_p->torus_auth_url, dummy_db, + torus_query, dummy_realm, m_p->proxy); + if (!doc) + { + // something fundamental broken in lookup. + *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; + *addinfo = odr_strdup(odr, "Torus server unavailable or " + "incorrectly configured"); + return; + } + const xmlNode *ptr = xmlDocGetRootElement(doc); + if (ptr && ptr->type == XML_ELEMENT_NODE) + { + if (strcmp((const char *) ptr->name, "records") == 0) + { + ptr = ptr->children; + while (ptr && ptr->type != XML_ELEMENT_NODE) + ptr = ptr->next; + } + if (ptr && strcmp((const char *) ptr->name, "record") == 0) + { + ptr = ptr->children; + while (ptr && ptr->type != XML_ELEMENT_NODE) + ptr = ptr->next; + } + if (ptr && strcmp((const char *) ptr->name, "layer") == 0) + { + ptr = ptr->children; + while (ptr && ptr->type != XML_ELEMENT_NODE) + ptr = ptr->next; + } + while (ptr) + { + if (ptr && ptr->type == XML_ELEMENT_NODE && + !strcmp((const char *) ptr->name, "identityId")) + break; + ptr = ptr->next; + } + } + if (!ptr) + { + *error = failure_code; + return; + } + session_realm = mp::xml::get_text(ptr); +} + void yf::Zoom::Impl::process(mp::Package &package) { FrontendPtr f = get_frontend(package); @@ -2201,9 +2594,20 @@ void yf::Zoom::Impl::process(mp::Package &package) *resp->preferredMessageSize = *req->preferredMessageSize; *resp->maximumRecordSize = *req->maximumRecordSize; - + + int error = 0; + char *addinfo = 0; + f->auth(package, req, &error, &addinfo, odr); + if (error) + { + resp->userInformationField = + zget_init_diagnostics(odr, error, addinfo); + *resp->result = 0; + package.session().close(); + } + else + f->m_is_virtual = true; package.response() = apdu; - f->m_is_virtual = true; } else package.move();