zoom: Honor extraArgs, strip # in zurl
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
index 41a8aca..9de8e6b 100644 (file)
@@ -76,6 +76,8 @@ namespace metaproxy_1 {
             std::string urlRecipe;
             std::string contentConnector;
             std::string sortStrategy;
+            std::string extraArgs;
+            std::string rpn2cql_fname;
             bool use_turbomarc;
             bool piggyback;
             CCL_bibset ccl_bibset;
@@ -98,6 +100,7 @@ namespace metaproxy_1 {
             bool enable_explain;
             xmlDoc *explain_doc;
             std::string m_proxy;
+            cql_transform_t cqlt;
         public:
             Backend();
             ~Backend();
@@ -117,16 +120,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);
@@ -207,6 +215,7 @@ 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<std::string,std::string> fieldmap;
             std::string xsldir;
@@ -284,6 +293,7 @@ yf::Zoom::Backend::Backend()
     enable_cproxy = true;
     enable_explain = false;
     explain_doc = 0;
+    cqlt = 0;
 }
 
 yf::Zoom::Backend::~Backend()
@@ -292,6 +302,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);
 }
@@ -349,8 +360,12 @@ void yf::Zoom::Backend::connect(std::string zurl,
                                 int *error, char **addinfo,
                                 ODR odr)
 {
+    size_t h = zurl.find_first_of('#');
+    if (h != std::string::npos)
+        zurl.erase(h);
     ZOOM_connection_connect(m_connection, zurl.length() ? zurl.c_str() : 0, 0);
     get_zoom_error(error, addinfo, odr);
+    
 }
 
 void yf::Zoom::Backend::search(ZOOM_query q, Odr_int *hits,
@@ -593,6 +608,13 @@ 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,
+                          "extraArgs"))
+        {
+            s->extraArgs = mp::xml::get_text(ptr);
+        }
+        else if (!strcmp((const char *) ptr->name, "rpn2cql"))
+            s->rpn2cql_fname = mp::xml::get_text(ptr);
     }
     return s;
 }
@@ -668,6 +690,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"))
@@ -1012,7 +1036,9 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     std::string authentication;
     std::string content_authentication;
     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;
@@ -1092,8 +1118,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;
@@ -1132,8 +1160,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;
 
@@ -1149,33 +1177,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);
     }
@@ -1256,11 +1302,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->m_proxy = proxy;
+    b->cqlt = cqlt;
     b->sptr = sptr;
     b->xsp = xsp;
     b->m_frontend_database = database;
@@ -1269,6 +1336,9 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
     if (sptr->query_encoding.length())
         b->set_option("rpnCharset", sptr->query_encoding);
 
+    if (sptr->extraArgs.length())
+        b->set_option("extraArgs", sptr->extraArgs);
+
     b->set_option("timeout", m_p->zoom_timeout.c_str());
     
     if (m_p->apdu_log) 
@@ -1284,6 +1354,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())
     {
@@ -1443,7 +1514,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,
@@ -1502,7 +1573,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,
@@ -1802,6 +1873,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)
 {
@@ -1828,7 +1900,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,
@@ -1843,8 +1914,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
         {
@@ -1945,8 +2016,8 @@ bool yf::Zoom::Frontend::retry(mp::Package &package,
             package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
             return true;
         }
-        error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
-        *addinfo = odr_strdup(odr, "proxy failure");
+        error = YAZ_BIB1_PROXY_FAILURE;
+        *addinfo = odr_strdup(odr, b->m_proxy.c_str());
     }
     else if (same_retries == 0 && proxy_retries == 0)
     {
@@ -2067,8 +2138,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;
@@ -2096,8 +2166,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));
@@ -2203,11 +2271,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)
         {
@@ -2380,6 +2444,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);
@@ -2423,9 +2608,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();