zoom: allow / in user and password db args MP-592
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
index 968b96b..67eb01e 100644 (file)
@@ -51,6 +51,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/sortspec.h>
 #include <yaz/tokenizer.h>
 #include <yaz/zoom.h>
+#include <yaz/otherinfo.h>
+#include <yaz/match_glob.h>
 
 namespace mp = metaproxy_1;
 namespace yf = mp::filter;
@@ -61,6 +63,7 @@ namespace metaproxy_1 {
           public:
             std::string authentication;
             std::string authenticationMode;
+            std::string contentAuthentication;
             std::string cfAuth;
             std::string cfProxy;
             std::string cfSubDB;
@@ -79,6 +82,7 @@ namespace metaproxy_1 {
             std::string sortStrategy;
             std::string extraArgs;
             std::string rpn2cql_fname;
+            std::string retry_on_failure;
             bool use_turbomarc;
             bool piggyback;
             CCL_bibset ccl_bibset;
@@ -102,6 +106,7 @@ namespace metaproxy_1 {
             xmlDoc *explain_doc;
             std::string m_proxy;
             cql_transform_t cqlt;
+            std::string retry_on_failure;
         public:
             Backend();
             ~Backend();
@@ -217,7 +222,9 @@ namespace metaproxy_1 {
             std::string torus_searchable_url;
             std::string torus_content_url;
             std::string torus_auth_url;
+            std::string torus_allow_ip;
             std::string default_realm;
+            std::string torus_auth_hostname;
             std::map<std::string,std::string> fieldmap;
             std::string xsldir;
             std::string file_path;
@@ -487,6 +494,7 @@ yf::Zoom::Searchable::Searchable(CCL_bibset base)
     piggyback = true;
     use_turbomarc = true;
     sortStrategy = "embed";
+    retry_on_failure = "1"; // existing default (should have been false)
     ccl_bibset = ccl_qual_dup(base);
 }
 
@@ -589,6 +597,11 @@ yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
             s->authenticationMode = mp::xml::get_text(ptr);
         }
         else if (!strcmp((const char *) ptr->name,
+                         "contentAuthentication"))
+        {
+            s->contentAuthentication = mp::xml::get_text(ptr);
+        }
+        else if (!strcmp((const char *) ptr->name,
                          "cfAuth"))
         {
             s->cfAuth = mp::xml::get_text(ptr);
@@ -698,6 +711,11 @@ yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
         }
         else if (!strcmp((const char *) ptr->name, "rpn2cql"))
             s->rpn2cql_fname = mp::xml::get_text(ptr);
+        else if (!strcmp((const char *) ptr->name,
+                          "retryOnFailure"))
+        {
+            s->retry_on_failure = mp::xml::get_text(ptr);
+        }
     }
     return s;
 }
@@ -775,8 +793,12 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only,
                     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, "allow_ip"))
+                    torus_allow_ip = 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, "auth_hostname"))
+                    torus_auth_hostname = mp::xml::get_text(attr->children);
                 else if (!strcmp((const char *) attr->name, "xsldir"))
                     xsldir = mp::xml::get_text(attr->children);
                 else if (!strcmp((const char *) attr->name, "element_transform"))
@@ -1118,8 +1140,6 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     else
         torus_db = database;
 
-    std::string authentication;
-    std::string content_authentication;
     std::string content_proxy;
     std::string realm = session_realm;
     if (realm.length() == 0)
@@ -1130,6 +1150,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     const char *param_content_user = 0;
     const char *param_content_password = 0;
     const char *param_nocproxy = 0;
+    const char *param_retry = 0;
     int no_parms = 0;
 
     char **names;
@@ -1165,6 +1186,8 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
             content_proxy = value;
         else if (!strcmp(name, "nocproxy"))
             param_nocproxy = value;
+        else if (!strcmp(name, "retry"))
+            param_retry = value;
         else if (!strcmp(name, "proxy"))
         {
             char **dstr;
@@ -1231,19 +1254,6 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
         return m_backend;
     }
 
-    if (param_user)
-    {
-        authentication = std::string(param_user);
-        if (param_password)
-            authentication += "/" + std::string(param_password);
-    }
-    if (param_content_user)
-    {
-        content_authentication = std::string(param_content_user);
-        if (param_content_password)
-            content_authentication += "/" + std::string(param_content_password);
-    }
-
     if (torus_db.compare("IR-Explain---1") == 0)
         return explain_search(package, database, error, addinfo, odr, torus_url,
                               torus_db, realm);
@@ -1256,15 +1266,17 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
         sptr = it->second;
     else if (torus_url.length() > 0)
     {
+        std::string torus_addinfo;
         std::string torus_query = "udb==" + torus_db;
         xmlDoc *doc = mp::get_searchable(package,torus_url, torus_db,
                                          torus_query,
-                                         realm, m_p->proxy);
+                                         realm, m_p->proxy,
+                                         torus_addinfo);
         if (!doc)
         {
             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
-            *addinfo = odr_strdup(odr, "Torus server unavailable or "
-                                  "incorrectly configured");
+            if (torus_addinfo.length())
+                *addinfo = odr_strdup(odr, torus_addinfo.c_str());
             BackendPtr b;
             return b;
         }
@@ -1423,6 +1435,11 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     b->m_frontend_database = database;
     b->enable_cproxy = param_nocproxy ? false : true;
 
+    if (param_retry)
+        b->retry_on_failure = param_retry;
+    else
+        b->retry_on_failure = b->sptr->retry_on_failure;
+
     if (sptr->query_encoding.length())
         b->set_option("rpnCharset", sptr->query_encoding);
 
@@ -1438,8 +1455,20 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
                                         maximumRecords > 0 */
     b->set_option("piggyback", sptr->piggyback ? "1" : "0");
 
-    if (authentication.length() == 0)
-        authentication = sptr->authentication;
+    std::string authentication = sptr->authentication;
+    if (param_user)
+    {
+        authentication = std::string(param_user);
+        if (param_password)
+            authentication += "/" + std::string(param_password);
+    }
+    std::string content_authentication = sptr->contentAuthentication;
+    if (param_content_user)
+    {
+        content_authentication = std::string(param_content_user);
+        if (param_content_password)
+            content_authentication += "/" + std::string(param_content_password);
+    }
 
     if (proxy.length() == 0)
         proxy = sptr->cfProxy;
@@ -1449,7 +1478,17 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     {
         // A CF target
         b->set_option("user", sptr->cfAuth);
-        if (authentication.length())
+        if (param_user)
+        {
+            out_names[no_out_args] = "user";
+            out_values[no_out_args++] = odr_strdup(odr, param_user);
+            if (param_password)
+            {
+                out_names[no_out_args] = "password";
+                out_values[no_out_args++] = odr_strdup(odr, param_password);
+            }
+        }
+        else if (authentication.length())
         {
             size_t found = authentication.find('/');
             if (found != std::string::npos)
@@ -1729,8 +1768,19 @@ Z_Records *yf::Zoom::Frontend::get_records(mp::Package &package,
     if (!*error)
     {
         for (i = 0; i < number_to_present; i++)
+        {
             if (!recs[i])
                 break;
+
+            const char *addinfo;
+            int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
+                                              &addinfo, 0 /* diagset */);
+            if (sur_error ==
+                YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS && addinfo &&
+                !strcmp(addinfo,
+                        "ZOOM C generated. Present phase and no records"))
+                break;
+        }
     }
     if (i > 0)
     {  // only return records if no error and at least one record
@@ -1761,7 +1811,7 @@ Z_Records *yf::Zoom::Frontend::get_records(mp::Package &package,
         npl->num_records = i;
         npl->records = (Z_NamePlusRecord **)
             odr_malloc(odr, i * sizeof(*npl->records));
-        for (i = 0; i < number_to_present; i++)
+        for (i = 0; i < npl->num_records; i++)
         {
             Z_NamePlusRecord *npr = 0;
             const char *addinfo;
@@ -2012,10 +2062,12 @@ 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_addinfo;
         std::string torus_query(query->u.type_104->u.cql);
         xmlDoc *doc = mp::get_searchable(package, torus_url, "",
                                          torus_query,
-                                         realm, m_p->proxy);
+                                         realm, m_p->proxy,
+                                         torus_addinfo);
         if (m_p->explain_xsp)
         {
             xmlDoc *rec_res =  xsltApplyStylesheet(m_p->explain_xsp, doc, 0);
@@ -2026,8 +2078,8 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package,
         if (!doc)
         {
             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
-            *addinfo = odr_strdup(odr, "Torus server unavailable or "
-                                  "incorrectly configured");
+            if (torus_addinfo.length())
+                *addinfo = odr_strdup(odr, torus_addinfo.c_str());
         }
         else
         {
@@ -2059,7 +2111,7 @@ 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)
+    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);
@@ -2131,7 +2183,8 @@ bool yf::Zoom::Frontend::retry(mp::Package &package,
         error = YAZ_BIB1_PROXY_FAILURE;
         *addinfo = odr_strdup(odr, b->m_proxy.c_str());
     }
-    else if (same_retries == 0 && proxy_retries == 0)
+    else if (b && b->retry_on_failure.compare("0")
+             && same_retries == 0 && proxy_retries == 0)
     {
         log_diagnostic(package, error, *addinfo);
         same_retries++;
@@ -2376,45 +2429,36 @@ next_proxy:
 
     if (b->get_option("sru"))
     {
-        int status = 0;
         Z_RPNQuery *zquery;
         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
-        mp::wrbuf wrb;
+        mp::wrbuf wrb_cql;
+        mp::wrbuf wrb_addinfo;
 
         if (!strcmp(b->get_option("sru"), "solr"))
-        {
-            solr_transform_t cqlt = solr_transform_create();
-
-            status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
-
-            solr_transform_close(cqlt);
-        }
+            error = solr_transform_rpn2solr_stream_r(b->cqlt, wrb_addinfo,
+                                                     wrbuf_vp_puts, wrb_cql,
+                                                     zquery);
         else
+            error = cql_transform_rpn2cql_stream_r(b->cqlt, wrb_addinfo,
+                                                   wrbuf_vp_puts, wrb_cql,
+                                                   zquery);
+        if (error)
         {
-            status = cql_transform_rpn2cql_wrbuf(b->cqlt, wrb, zquery);
-        }
-        if (status == 0)
-        {
-            ZOOM_query_cql(q, wrbuf_cstr(wrb));
-            package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb));
-            b->search(q, &hits, &error, &addinfo, &fl, odr);
-        }
-        ZOOM_query_destroy(q);
-
-        if (status)
-        {
-            error = YAZ_BIB1_MALFORMED_QUERY;
-            const char *addinfo = "can not convert from RPN to CQL/Solr";
-            log_diagnostic(package, error, addinfo);
-            apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
+            log_diagnostic(package, error, wrb_addinfo.c_str_null());
+            apdu_res = odr.create_searchResponse(apdu_req, error,
+                                                 wrb_addinfo.c_str_null());
             package.response() = apdu_res;
             return;
         }
+        ZOOM_query_cql(q, wrb_cql.c_str());
+        package.log("zoom", YLOG_LOG, "search CQL: %s", wrb_cql.c_str());
+        b->search(q, &hits, &error, &addinfo, &fl, odr);
+        ZOOM_query_destroy(q);
     }
     else
     {
-        ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf));
-        package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
+        ZOOM_query_prefix(q, pqf_wrbuf.c_str());
+        package.log("zoom", YLOG_LOG, "search PQF: %s", pqf_wrbuf.c_str());
         b->search(q, &hits, &error, &addinfo, &fl, odr);
         ZOOM_query_destroy(q);
     }
@@ -2614,9 +2658,32 @@ void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req,
         }
     }
 
-    std::string ip = package.origin().get_address();
+    Z_OtherInformation **oi = &req->otherInfo;
+    const char *ip_cstr =
+        yaz_oi_get_string_oid(oi, yaz_oid_userinfo_client_ip, 1, 0);
+    std::string ip;
+    if (ip_cstr)
+        ip = ip_cstr;
+    else
+        ip = package.origin().get_address();
+
     yaz_log(YLOG_LOG, "IP=%s", ip.c_str());
 
+    {
+        NMEM nmem = nmem_create();
+        char **darray;
+        int i, num;
+        nmem_strsplit_blank(nmem, m_p->torus_allow_ip.c_str(), &darray, &num);
+        for (i = 0; i < num; i++)
+        {
+            yaz_log(YLOG_LOG, "check against %s+%s", darray[i], ip.c_str());
+            if (yaz_match_glob(darray[i], ip.c_str()))
+                break;
+        }
+        nmem_destroy(nmem);
+        if (i < num)
+            return;  /* allow this IP */
+    }
     std::string torus_query;
     int failure_code;
 
@@ -2628,27 +2695,31 @@ void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req,
     }
     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 = "ipRanges encloses/net.ipaddress \"";
+        torus_query += escape_cql_term(std::string(ip));
         torus_query += "\"";
+
+        if (m_p->torus_auth_hostname.length())
+        {
+            torus_query += " AND hostName == \"";
+            torus_query += escape_cql_term(m_p->torus_auth_hostname);
+            torus_query += "\"";
+        }
         failure_code = YAZ_BIB1_INIT_AC_BLOCKED_NETWORK_ADDRESS;
     }
 
     std::string dummy_db;
     std::string dummy_realm;
+    std::string torus_addinfo;
     xmlDoc *doc = mp::get_searchable(package, m_p->torus_auth_url, dummy_db,
-                                     torus_query, dummy_realm, m_p->proxy);
+                                     torus_query, dummy_realm, m_p->proxy,
+                                     torus_addinfo);
     if (!doc)
     {
         // something fundamental broken in lookup.
         *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
-        *addinfo = odr_strdup(odr, "zoom: torus server unavailable or "
-                              "incorrectly configured.");
+        if (torus_addinfo.length())
+            *addinfo = odr_strdup(odr, torus_addinfo.c_str());
         return;
     }
     const xmlNode *ptr = xmlDocGetRootElement(doc);