Fixed bug #189: Bad authenticated session transfer.
[yazproxy-moved-to-github.git] / src / yaz-proxy.cpp
index 0ac056b..66d7da9 100644 (file)
@@ -1,14 +1,14 @@
-/* $Id: yaz-proxy.cpp,v 1.3 2004-04-11 12:25:01 adam Exp $
+/* $Id: yaz-proxy.cpp,v 1.10 2004-10-23 23:12:24 adam Exp $
    Copyright (c) 1998-2004, Index Data.
 
 This file is part of the yaz-proxy.
 
-Zebra is free software; you can redistribute it and/or modify it under
+YAZ proxy is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
 Software Foundation; either version 2, or (at your option) any later
 version.
 
-Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
@@ -24,6 +24,10 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <unistd.h>
 #endif
 
+#if HAVE_GETTIMEOFDAY
+#include <sys/time.h>
+#endif
+
 #include <assert.h>
 #include <time.h>
 #include <sys/types.h>
@@ -36,6 +40,14 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/diagbib1.h>
 #include <yazproxy/proxy.h>
 #include <yaz/pquery.h>
+#include <yaz/otherinfo.h>
+
+#if HAVE_XSLT
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxslt/xsltutils.h>
+#include <libxslt/transform.h>
+#endif
 
 static const char *apdu_name(Z_APDU *apdu)
 {
@@ -130,6 +142,9 @@ Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
     m_s2z_stylesheet = 0;
     m_s2z_database = 0;
     m_schema = 0;
+    m_backend_type = 0;
+    m_backend_charset = 0;
+    m_frontend_type = 0;
     m_initRequest_apdu = 0;
     m_initRequest_mem = 0;
     m_initRequest_preferredMessageSize = 0;
@@ -148,8 +163,12 @@ Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
     m_soap_ns = 0;
     m_s2z_packing = Z_SRW_recordPacking_string;
 #if HAVE_GETTIMEOFDAY
-    m_time_tv.tv_sec = 0;
-    m_time_tv.tv_usec = 0;
+    m_time_tv = xmalloc(sizeof(struct timeval));
+    struct timeval *tv = (struct timeval *) m_time_tv;
+    tv->tv_sec = 0;
+    tv->tv_usec = 0;
+#else
+    m_time_tv = 0;
 #endif
     if (!m_parent)
        low_socket_open();
@@ -163,16 +182,20 @@ Yaz_Proxy::~Yaz_Proxy()
     nmem_destroy(m_mem_invalid_session);
     nmem_destroy(m_referenceId_mem);
 
-    xfree (m_proxyTarget);
-    xfree (m_default_target);
-    xfree (m_proxy_authentication);
-    xfree (m_optimize);
+    xfree(m_proxyTarget);
+    xfree(m_default_target);
+    xfree(m_proxy_authentication);
+    xfree(m_optimize);
 
 #if HAVE_XSLT
     if (m_stylesheet_xsp)
-       xsltFreeStylesheet(m_stylesheet_xsp);
+       xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
 #endif
+    xfree (m_time_tv);
+
     xfree (m_schema);
+    xfree (m_backend_type);
+    xfree (m_backend_charset);
     if (m_s2z_odr_init)
        odr_destroy(m_s2z_odr_init);
     if (m_s2z_odr_search)
@@ -368,10 +391,11 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
 #endif
            xfree(m_default_target);
            m_default_target = xstrdup(proxy_host);
-           proxy_host = m_default_target;
        }
+       proxy_host = m_default_target;
        int client_idletime = -1;
        const char *cql2rpn_fname = 0;
+       const char *authentication = 0;
        url[0] = m_default_target;
        url[1] = 0;
        if (cfg)
@@ -384,7 +408,8 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
                                 &m_keepalive_limit_bw,
                                 &m_keepalive_limit_pdu,
                                 &pre_init,
-                                &cql2rpn_fname);
+                                &cql2rpn_fname,
+                                &authentication);
        }
        if (client_idletime != -1)
        {
@@ -393,6 +418,8 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
        }
        if (cql2rpn_fname)
            m_cql2rpn.set_pqf_file(cql2rpn_fname);
+       if (authentication)
+           set_proxy_authentication(authentication);
        if (!url[0])
        {
            yaz_log(LOG_LOG, "%sNo default target", m_session_str);
@@ -450,15 +477,17 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
            }
        }
     }
-    else if (!c)
+    else if (!c && apdu->which == Z_APDU_initRequest
+            && apdu->u.initRequest->idAuthentication == 0)
     {
-       // don't have a client session yet. Search in session w/o cookie
+       // anonymous sessions without cookie.
+       // if authentication is set it is NOT anonymous se we can't share them.
        for (c = parent->m_clientPool; c; c = c->m_next)
        {
-           assert (c->m_prev);
-           assert (*c->m_prev == c);
+           assert(c->m_prev);
+           assert(*c->m_prev == c);
            if (c->m_server == 0 && c->m_cookie == 0 && 
-               c->m_waiting == 0 &&
+               c->m_waiting == 0 && 
                !strcmp(m_proxyTarget, c->get_hostname()))
            {
                // found it in cache
@@ -505,6 +534,12 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
                     odr_strdup (odr_encode(), m_proxy_authentication);
             }
         }
+       else
+       {
+           // the client use authentication. We set the keepalive PDU
+           // to 0 so we don't cache it in releaseClient
+           m_keepalive_limit_pdu = 0;
+       }
        // go through list of clients - and find the lowest/oldest one.
        Yaz_ProxyClient *c_min = 0;
        int min_seq = -1;
@@ -671,7 +706,8 @@ void Yaz_Proxy::convert_xsl_delay()
            
            yaz_log(LOG_LOG, "%sXSLT convert %d",
                    m_session_str, m_stylesheet_offset);
-           res = xsltApplyStylesheet(m_stylesheet_xsp, doc, 0);
+           res = xsltApplyStylesheet((xsltStylesheetPtr) m_stylesheet_xsp,
+                                     doc, 0);
 
            if (res)
            {
@@ -697,7 +733,7 @@ void Yaz_Proxy::convert_xsl_delay()
        m_stylesheet_nprl = 0;
 #if HAVE_XSLT
        if (m_stylesheet_xsp)
-           xsltFreeStylesheet(m_stylesheet_xsp);
+           xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
 #endif
        m_stylesheet_xsp = 0;
        timeout(m_client_idletime);
@@ -707,11 +743,38 @@ void Yaz_Proxy::convert_xsl_delay()
        timeout(0);
 }
 
-void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
+void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p)
+{
+    if (m_frontend_type != VAL_NONE)
+    {
+       int i;
+       for (i = 0; i < p->num_records; i++)
+       {
+           Z_NamePlusRecord *npr = p->records[i];
+           if (npr->which == Z_NamePlusRecord_databaseRecord)
+           {
+               Z_External *r = npr->u.databaseRecord;
+               if (r->which == Z_External_octet)
+               {
+                   npr->u.databaseRecord =
+                       z_ext_record(odr_encode(),
+                                    m_frontend_type,
+                                    (char*) r->u.octet_aligned->buf,
+                                    r->u.octet_aligned->len);
+               }
+           }
+       }
+    }
+}
+
+void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p,
+                                  const char *backend_charset)
 {
     int i;
 
-    yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC-8");
+    if (!backend_charset)
+       backend_charset = "MARC-8";
+    yaz_iconv_t cd = yaz_iconv_open("UTF-8", backend_charset);
     yaz_marc_t mt = yaz_marc_create();
     yaz_marc_xml(mt, YAZ_MARC_MARCXML);
     yaz_marc_iconv(mt, cd);
@@ -744,18 +807,19 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p)
 void Yaz_Proxy::logtime()
 {
 #if HAVE_GETTIMEOFDAY
-    if (m_time_tv.tv_sec)
+    struct timeval *tv = (struct timeval*) m_time_tv;
+    if (tv->tv_sec)
     {
-       struct timeval tv;
-       gettimeofday(&tv, 0);
-       long diff = (tv.tv_sec - m_time_tv.tv_sec)*1000000 +
-           (tv.tv_usec - m_time_tv.tv_usec);
+       struct timeval tv1;
+       gettimeofday(&tv1, 0);
+       long diff = (tv1.tv_sec - tv->tv_sec)*1000000 +
+           (tv1.tv_usec - tv->tv_usec);
        if (diff >= 0)
            yaz_log(LOG_LOG, "%sElapsed %ld.%03ld", m_session_str,
                    diff/1000000, (diff/1000)%1000);
     }
-    m_time_tv.tv_sec = 0;
-    m_time_tv.tv_usec = 0;
+    tv->tv_sec = 0;
+    tv->tv_usec = 0;
 #endif
 }
 
@@ -924,8 +988,8 @@ int Yaz_Proxy::send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
     if (cfg)
     {
        int len;
-       char *b = cfg->get_explain(odr_encode(), 0 /* target */,
-                                  m_s2z_database, &len);
+       char *b = cfg->get_explain_doc(odr_encode(), 0 /* target */,
+                                      m_s2z_database, &len);
        if (b)
        {
            Z_SRW_PDU *res = yaz_srw_get(odr_encode(), Z_SRW_explain_response);
@@ -1043,8 +1107,11 @@ int Yaz_Proxy::send_to_client(Z_APDU *apdu)
        {
            if (p && p->which == Z_Records_DBOSD)
            {
+               if (m_backend_type)
+                   convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
                if (m_marcxml_flag)
-                   convert_to_marcxml(p->u.databaseOrSurDiagnostics);
+                   convert_to_marcxml(p->u.databaseOrSurDiagnostics,
+                                      m_backend_charset);
                if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
                    return 0;
                    
@@ -1081,8 +1148,11 @@ int Yaz_Proxy::send_to_client(Z_APDU *apdu)
        }
        if (p && p->which == Z_Records_DBOSD)
        {
+           if (m_backend_type)
+               convert_to_frontend_type(p->u.databaseOrSurDiagnostics);
            if (m_marcxml_flag)
-               convert_to_marcxml(p->u.databaseOrSurDiagnostics);
+               convert_to_marcxml(p->u.databaseOrSurDiagnostics,
+                                  m_backend_charset);
            if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu))
                return 0;
        }
@@ -1189,6 +1259,11 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
                return 0;
            }
            Z_NamePlusRecordList *npr;
+#if 0
+           yaz_log(LOG_LOG, "%sCache lookup %d+%d syntax=%s",
+                   m_session_str, start, toget, yaz_z3950oid_to_str(
+                       pr->preferredRecordSyntax, &oclass));
+#endif
            if (m_client->m_cache.lookup (odr_encode(), &npr, start, toget,
                                          pr->preferredRecordSyntax,
                                          pr->recordComposition))
@@ -1408,7 +1483,7 @@ void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len)
     m_pdu_stat.add_bytes(1);
 
 #if HAVE_GETTIMEOFDAY
-    gettimeofday(&m_time_tv, 0);
+    gettimeofday((struct timeval *) m_time_tv, 0);
 #endif
 
     int bw_total = m_bw_stat.get_total();
@@ -1574,19 +1649,29 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
            rc = &rc_temp;
        }
 
+       if (sr->preferredRecordSyntax)
+       {
+           struct oident *ent;
+           ent = oid_getentbyoid(sr->preferredRecordSyntax);
+           m_frontend_type = ent->value;
+       }
+       else
+           m_frontend_type = VAL_NONE;
+
        char *stylesheet_name = 0;
        if (cfg)
            err = cfg->check_syntax(odr_encode(),
                                    m_default_target,
                                    sr->preferredRecordSyntax, rc,
-                                   &addinfo, &stylesheet_name, &m_schema);
+                                   &addinfo, &stylesheet_name, &m_schema,
+                                   &m_backend_type, &m_backend_charset);
        if (stylesheet_name)
        {
            m_parent->low_socket_close();
 
 #if HAVE_XSLT
            if (m_stylesheet_xsp)
-               xsltFreeStylesheet(m_stylesheet_xsp);
+               xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
            m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
                                                       stylesheet_name);
 #endif
@@ -1597,8 +1682,19 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
        }
        if (err == -1)
        {
-           sr->preferredRecordSyntax =
-               yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC);
+           sr->smallSetElementSetNames = 0;
+           sr->mediumSetElementSetNames = 0;
+           if (m_backend_type)
+           {
+               
+               sr->preferredRecordSyntax =
+                   yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN,
+                                       m_backend_type);
+           }
+           else
+               sr->preferredRecordSyntax =
+                   yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN,
+                                          VAL_USMARC);
            m_marcxml_flag = 1;
        }
        else if (err)
@@ -1614,6 +1710,11 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
            
            return 0;
        }
+       else if (m_backend_type)
+       {
+           sr->preferredRecordSyntax =
+               yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type);
+       }
     }
     else if (apdu->which == Z_APDU_presentRequest)
     {
@@ -1622,20 +1723,29 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
        char *addinfo = 0;
        Yaz_ProxyConfig *cfg = check_reconfigure();
 
+       if (pr->preferredRecordSyntax)
+       {
+           struct oident *ent;
+           ent = oid_getentbyoid(pr->preferredRecordSyntax);
+           m_frontend_type = ent->value;
+       }
+       else
+           m_frontend_type = VAL_NONE;
+
        char *stylesheet_name = 0;
        if (cfg)
            err = cfg->check_syntax(odr_encode(), m_default_target,
                                    pr->preferredRecordSyntax,
                                    pr->recordComposition,
-                                   &addinfo, &stylesheet_name, &m_schema);
+                                   &addinfo, &stylesheet_name, &m_schema,
+                                   &m_backend_type, &m_backend_charset);
        if (stylesheet_name)
        {
            m_parent->low_socket_close();
 
 #if HAVE_XSLT
            if (m_stylesheet_xsp)
-               xsltFreeStylesheet(m_stylesheet_xsp);
-
+               xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp);
            m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*)
                                                       stylesheet_name);
 #endif
@@ -1646,8 +1756,18 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
        }
        if (err == -1)
        {
-           pr->preferredRecordSyntax =
-               yaz_oidval_to_z3950oid(odr_decode(), CLASS_RECSYN, VAL_USMARC);
+           pr->recordComposition = 0;
+           if (m_backend_type)
+           {
+               
+               pr->preferredRecordSyntax =
+                   yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN,
+                                       m_backend_type);
+           }
+           else
+               pr->preferredRecordSyntax =
+                   yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN,
+                                          VAL_USMARC);
            m_marcxml_flag = 1;
        }
        else if (err)
@@ -1664,6 +1784,11 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
            
            return 0;
        }
+       else if (m_backend_type)
+       {
+           pr->preferredRecordSyntax =
+               yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type);
+       }
     }
     return apdu;
 }
@@ -1679,6 +1804,25 @@ Z_ElementSetNames *Yaz_Proxy::mk_esn_from_schema(ODR o, const char *schema)
     return esn;
 }
 
+void Yaz_Proxy::srw_get_client(const char *db, const char **backend_db)
+{
+    const char *t = 0;
+    Yaz_ProxyConfig *cfg = check_reconfigure();
+    if (cfg)
+       t = cfg->get_explain_name(db, backend_db);
+
+    if (m_client && m_default_target && t && strcmp(m_default_target, t))
+    {
+       releaseClient();
+    }
+    
+    if (t)
+    {
+       xfree(m_default_target);
+       m_default_target = xstrdup(t);
+    }
+}
+       
 void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
 {
     if (m_s2z_odr_init)
@@ -1736,6 +1880,9 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
        {
            Z_SRW_searchRetrieveRequest *srw_req = srw_pdu->u.request;
 
+           const char *backend_db = srw_req->database;
+           srw_get_client(srw_req->database, &backend_db);
+
            m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
            // recordXPath unsupported.
            if (srw_req->recordXPath)
@@ -1786,7 +1933,7 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
            z_searchRequest->databaseNames = (char**)
                odr_malloc(m_s2z_odr_search, sizeof(char *));
            z_searchRequest->databaseNames[0] = odr_strdup(m_s2z_odr_search,
-                                                          srw_req->database);
+                                                          backend_db);
            
            // query transformation
            Z_Query *query = (Z_Query *)
@@ -1913,6 +2060,9 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
        {
            Z_SRW_explainRequest *srw_req = srw_pdu->u.explain_request;
 
+           const char *backend_db = srw_req->database;
+           srw_get_client(srw_req->database, &backend_db);
+
            m_s2z_database = odr_strdup(m_s2z_odr_init, srw_req->database);
 
            // save stylesheet
@@ -2132,8 +2282,10 @@ void Yaz_Proxy::connectNotify()
 {
 }
 
-void Yaz_Proxy::shutdown()
+void Yaz_Proxy::releaseClient()
 {
+    xfree(m_proxyTarget);
+    m_proxyTarget = 0;
     m_invalid_session = 0;
     // only keep if keep_alive flag is set...
     if (m_client && 
@@ -2151,7 +2303,7 @@ void Yaz_Proxy::shutdown()
         assert (m_client->m_waiting != 2);
        // Tell client (if any) that no server connection is there..
        m_client->m_server = 0;
-       m_invalid_session = 0;
+       m_client = 0;
     }
     else if (m_client)
     {
@@ -2160,6 +2312,7 @@ void Yaz_Proxy::shutdown()
                  m_client->get_hostname());
         assert (m_client->m_waiting != 2);
        delete m_client;
+       m_client = 0;
     }
     else if (!m_parent)
     {
@@ -2174,6 +2327,11 @@ void Yaz_Proxy::shutdown()
     }
     if (m_parent)
        m_parent->pre_init();
+}
+
+void Yaz_Proxy::shutdown()
+{
+    releaseClient();
     delete this;
 }
 
@@ -2276,6 +2434,7 @@ void Yaz_Proxy::pre_init()
     int keepalive_limit_bw, keepalive_limit_pdu;
     int pre_init;
     const char *cql2rpn = 0;
+    const char *authentication = 0;
 
     Yaz_ProxyConfig *cfg = check_reconfigure();
 
@@ -2293,7 +2452,8 @@ void Yaz_Proxy::pre_init()
                                          &keepalive_limit_bw,
                                          &keepalive_limit_pdu,
                                          &pre_init,
-                                         &cql2rpn) ; i++)
+                                         &cql2rpn,
+                                         &authentication) ; i++)
     {
        if (pre_init)
        {