Z39.50 authentication user is no longer set to client-IP for SRU.
[yazproxy-moved-to-github.git] / src / yaz-proxy.cpp
index 6b04f38..6abcbd7 100644 (file)
@@ -1,7 +1,5 @@
-/* $Id: yaz-proxy.cpp,v 1.75 2007-04-30 19:46:34 adam Exp $
-   Copyright (c) 1998-2007, Index Data.
-
-This file is part of the yazproxy.
+/* This file is part of YAZ proxy
+   Copyright (C) 1998-2008 Index Data
 
 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
@@ -14,10 +12,9 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with YAZ proxy; see the file LICENSE.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
- */
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
 
 #ifdef WIN32
 #define HAVE_SYS_STAT_H 1
@@ -220,6 +217,7 @@ Yaz_Proxy::Yaz_Proxy(IPDU_Observable *the_PDU_Observable,
     m_seed = time(0);
     m_client_idletime = 600;
     m_target_idletime = 600;
+    m_max_sockets = 1024;
     m_optimize = xstrdup ("1");
     strcpy(m_session_str, "0 ");
     m_session_no = 0;
@@ -399,7 +397,6 @@ Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
     if (m_reconfig_flag)
     {
         yaz_log(YLOG_LOG, "reconfigure");
-        yaz_log_reopen();
         if (m_config_fname && cfg)
         {
             yaz_log(YLOG_LOG, "reconfigure config %s", m_config_fname);
@@ -487,7 +484,7 @@ char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
     Z_OtherInformationUnit *oi =
         update_otherInformation(otherInfo, 0, yaz_oid_userinfo_cookie, 1, 1);
     
-    if (oi->which == Z_OtherInfo_characterInfo)
+    if (oi && oi->which == Z_OtherInfo_characterInfo)
         return oi->information.characterInfo;
     return 0;
 }
@@ -497,7 +494,7 @@ char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
     Z_OtherInformationUnit *oi =
         update_otherInformation(otherInfo, 0, yaz_oid_userinfo_proxy, 1, 1);
     
-    if (oi->which == Z_OtherInfo_characterInfo)
+    if (oi && oi->which == Z_OtherInfo_characterInfo)
         return oi->information.characterInfo;
     return 0;
 }
@@ -547,8 +544,27 @@ const char *Yaz_Proxy::load_balance(const char **url)
     return ret_min;
 }
 
+int Yaz_Proxy::get_number_of_connections()
+{
+    int no_connections = 0;
+    Yaz_ProxyClient *c;
+
+    for (c = m_parent->m_clientPool; c; c = c->m_next)
+    {
+        assert(c->m_prev);
+        assert(*c->m_prev == c);
+        if (!strcmp(m_proxyTarget, c->get_hostname()))
+        {
+            no_connections++;
+        }
+    }
+    yaz_log (YLOG_LOG, "%sExisting %s connections: %d", m_session_str, m_proxyTarget,
+        no_connections);
+    return no_connections;
+}
+
 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
-                                       const char *proxy_host)
+                                       const char *proxy_host, int *http_code)
 {
     assert (m_parent);
     Yaz_Proxy *parent = m_parent;
@@ -585,6 +601,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
                                  &m_pdu_max, &m_max_record_retrieve,
                                  &m_search_max,
                                  &m_target_idletime, &client_idletime,
+                                 &m_max_sockets,
                                  &parent->m_max_clients,
                                  &m_keepalive_limit_bw,
                                  &m_keepalive_limit_pdu,
@@ -680,7 +697,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
         {
             assert(c->m_prev);
             assert(*c->m_prev == c);
-            if (c->m_server == 0 && c->m_cookie == 0 &&  c->m_waiting == 0 
+            if (c->m_server == 0 && c->m_cookie == 0 && c->m_waiting == 0 
                 && c->compare_idAuthentication(apdu)
                 && c->compare_charset(apdu)
                 && !strcmp(m_proxyTarget, c->get_hostname()))
@@ -711,6 +728,16 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
         if (apdu->which != Z_APDU_initRequest)
         {
             yaz_log (YLOG_LOG, "%sno init request as first PDU", m_session_str);
+            *http_code = 500;
+            return 0;
+        }
+
+        int no_in_use = get_number_of_connections();
+        if (no_in_use >= m_max_sockets)
+        {
+            yaz_log (YLOG_LOG, "%smax sockets reached %d", m_session_str,
+                m_max_sockets);
+            *http_code = 500;
             return 0;
         }
         // go through list of clients - and find the lowest/oldest one.
@@ -770,6 +797,7 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie,
         }
         else
         {
+
             yaz_log (YLOG_LOG, "%sNEW %d %s",
                      m_session_str, parent->m_seqno, m_proxyTarget);
             c = new Yaz_ProxyClient(m_PDU_Observable->clone(), parent);
@@ -930,53 +958,30 @@ void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p)
                 Z_External *r = npr->u.databaseRecord;
                 if (r->which == Z_External_octet)
                 {
-#if HAVE_USEMARCON
+#if !HAVE_USEMARCON
                     if (m_usemarcon_ini_stage1 && *m_usemarcon_ini_stage1)
+                        yaz_log (YLOG_LOG, "%sError: USEMARCON requested but not available",
+                            m_session_str);
+#endif
+#if HAVE_USEMARCON
+                    yaz_log (YLOG_DEBUG, "%sUSEMARCON stage1=%s stage2=%s",
+                        m_session_str,
+                        m_usemarcon_ini_stage1 ? m_usemarcon_ini_stage1 : "(none)",
+                        m_usemarcon_ini_stage2 ? m_usemarcon_ini_stage2 : "(none)");
+                    char *converted;
+                    int convlen;
+                    if (m_usemarcon->convert(m_usemarcon_ini_stage1, m_usemarcon_ini_stage2,
+                        (char*) r->u.octet_aligned->buf, r->u.octet_aligned->len,
+                        &converted, &convlen))
                     {
-                        if (!m_usemarcon->m_stage1)
-                        {
-                            m_usemarcon->m_stage1 = new CDetails();
-                        }
-                        m_usemarcon->m_stage1->SetIniFileName(m_usemarcon_ini_stage1);
-                        m_usemarcon->m_stage1->SetMarcRecord((char*) r->u.octet_aligned->buf, r->u.octet_aligned->len);
-                        int res = m_usemarcon->m_stage1->Start();
-                        if (res == 0)
-                        {
-                            char *converted;
-                            int convlen;
-                            m_usemarcon->m_stage1->GetMarcRecord(converted, convlen);
-                            if (m_usemarcon_ini_stage2 && *m_usemarcon_ini_stage2)
-                            {
-                                if (!m_usemarcon->m_stage2)
-                                {
-                                    m_usemarcon->m_stage2 = new CDetails();
-                                }
-                                m_usemarcon->m_stage2->SetIniFileName(m_usemarcon_ini_stage2);
-                                m_usemarcon->m_stage2->SetMarcRecord(converted, convlen);
-                                res = m_usemarcon->m_stage2->Start();
-                                if (res == 0)
-                                {
-                                    free(converted);
-                                    m_usemarcon->m_stage2->GetMarcRecord(converted, convlen);
-                                }
-                                else
-                                {
-                                    yaz_log(YLOG_LOG, "%sUSEMARCON stage 2 error %d", m_session_str, res);
-                                }
-                            }
-                            npr->u.databaseRecord =
-                                z_ext_record_oid(odr_encode(),
-                                                 m_frontend_type,
-                                                 converted,
-                                                 strlen(converted));
-                            free(converted);
-                        }
-                        else
-                        {
-                            yaz_log(YLOG_LOG, "%sUSEMARCON stage 1 error %d", m_session_str, res);
-                        }
-                        continue;
+                        npr->u.databaseRecord =
+                            z_ext_record_oid(odr_encode(),
+                                             m_frontend_type,
+                                             converted,
+                                             strlen(converted));
+                        free(converted);
                     }
+                    else
 #endif
 /* HAVE_USEMARCON */
                     npr->u.databaseRecord =
@@ -1011,7 +1016,7 @@ void Yaz_Proxy::convert_records_charset(Z_NamePlusRecordList *p,
             if (npr->which == Z_NamePlusRecord_databaseRecord)
             {
                 Z_External *r = npr->u.databaseRecord;
-                const int *oid = r->direct_reference;
+                const Odr_oid *oid = r->direct_reference;
                 if (!oid)
                     continue;
 
@@ -2796,18 +2801,6 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq)
                 }
                 auth->u.idPass->userId = odr_strdup(m_s2z_odr_init, authorization_str);
             }
-            else
-            {
-                // Use _client_ IP as shown in the log entries...!
-                yaz_log(YLOG_LOG, "No authorization_str present: use client IP: %s\n", m_peername);
-                
-                auth = (Z_IdAuthentication *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdAuthentication));
-                   auth->which = Z_IdAuthentication_idPass;
-                   auth->u.idPass = (Z_IdPass *) odr_malloc(m_s2z_odr_init, sizeof(Z_IdPass));
-                   auth->u.idPass->groupId  = NULL;
-                   auth->u.idPass->password = NULL;
-                   auth->u.idPass->userId   = odr_strdup(m_s2z_odr_init, m_peername);
-            }
         }              
         
         if (srw_pdu->which == Z_SRW_searchRetrieve_request)
@@ -3250,12 +3243,13 @@ void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
     // Determine our client.
     Z_OtherInformation **oi;
     get_otherInfoAPDU(apdu, &oi);
-    m_client = get_client(apdu, get_cookie(oi), get_proxy(oi));
+    int http_code = 404;
+    m_client = get_client(apdu, get_cookie(oi), get_proxy(oi), &http_code);
     if (!m_client)
     {
         if (m_http_version)
         {   // HTTP. Send not found
-            send_http_response(404);
+            send_http_response(http_code);
             return;
         }
         else
@@ -3509,6 +3503,7 @@ void Yaz_Proxy::pre_init()
     const char *zurl_in_use[MAX_ZURL_PLEX];
     int limit_bw, limit_pdu, limit_req, limit_search;
     int target_idletime, client_idletime;
+    int max_sockets;
     int max_clients;
     int keepalive_limit_bw, keepalive_limit_pdu;
     int pre_init;
@@ -3530,6 +3525,7 @@ void Yaz_Proxy::pre_init()
                                           &limit_bw, &limit_pdu, &limit_req,
                                           &limit_search,
                                           &target_idletime, &client_idletime,
+                                          &max_sockets,
                                           &max_clients,
                                           &keepalive_limit_bw,
                                           &keepalive_limit_pdu,
@@ -3573,7 +3569,8 @@ void Yaz_Proxy::pre_init()
                         "sparew=%d preinit=%d",m_session_str,
                         name, zurl_in_use[j], in_use, other,
                         spare, spare_waiting, pre_init);
-                if (spare + spare_waiting < pre_init)
+                if (spare + spare_waiting < pre_init
+                    && in_use + spare + spare_waiting + other < max_sockets)
                 {
                     c = new Yaz_ProxyClient(m_PDU_Observable->clone(), this);
                     c->m_next = m_clientPool;