Implemented plugin facility. First use is authentication from
[yazproxy-moved-to-github.git] / src / yaz-proxy.cpp
index e97946a..4400bbb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: yaz-proxy.cpp,v 1.20 2005-02-10 08:09:42 oleg Exp $
+/* $Id: yaz-proxy.cpp,v 1.22 2005-02-11 15:19:08 adam Exp $
    Copyright (c) 1998-2005, Index Data.
 
 This file is part of the yaz-proxy.
@@ -249,11 +249,12 @@ void Yaz_Proxy::set_proxy_authentication (const char *auth)
     if (auth)
        m_proxy_authentication = (char *) xstrdup (auth);
 }
+
 void Yaz_Proxy::set_proxy_negotiation (const char *charset, const char *lang)
 {
-    yaz_log(YLOG_LOG, "%sSet the proxy negotiation: charset to '%s',
-       language to '%s'", m_session_str, (charset)?charset:"none",
-       (lang)?lang:"none");
+    yaz_log(YLOG_LOG, "%sSet the proxy negotiation: charset to '%s', "
+       "language to '%s'", m_session_str, charset?charset:"none",
+       lang?lang:"none");
     xfree (m_proxy_negotiation_charset);
     xfree (m_proxy_negotiation_lang);
     m_proxy_negotiation_charset = m_proxy_negotiation_lang = 0;
@@ -262,6 +263,7 @@ void Yaz_Proxy::set_proxy_negotiation (const char *charset, const char *lang)
     if (lang)
        m_proxy_negotiation_lang = (char *) xstrdup (lang);
 }
+
 Yaz_ProxyConfig *Yaz_Proxy::check_reconfigure()
 {
     if (m_parent)
@@ -1667,7 +1669,6 @@ void Yaz_Proxy::handle_charset_lang_negotiation(Z_APDU *apdu)
     {
        Z_InitResponse *initResponse = apdu->u.initResponse;    
        Z_OtherInformation **otherInfo;  
-       Z_OtherInformationUnit *oi;
        
        if (ODR_MASK_GET(initResponse->options, Z_Options_negotiationModel))
        {
@@ -1836,6 +1837,41 @@ Z_APDU *Yaz_Proxy::handle_query_validation(Z_APDU *apdu)
     return apdu;
 }
 
+int Yaz_Proxy::handle_authentication(Z_APDU *apdu)
+{
+    if (apdu->which != Z_APDU_initRequest)
+       return 1;  // pass if no init request
+    Z_InitRequest *req = apdu->u.initRequest;
+
+    Yaz_ProxyConfig *cfg = check_reconfigure();
+    if (!cfg)
+       return 1;  // pass if no config
+
+    int ret;
+    if (req->idAuthentication == 0)
+    {
+       ret = cfg->check_authentication(0, 0, 0);
+    }
+    else if (req->idAuthentication->which == Z_IdAuthentication_idPass)
+    {
+       ret = cfg->check_authentication(
+           req->idAuthentication->u.idPass->userId,
+           req->idAuthentication->u.idPass->groupId,
+           req->idAuthentication->u.idPass->password);
+    }
+    else if (req->idAuthentication->which == Z_IdAuthentication_open)
+    {
+       char user[64], pass[64];
+       *user = '\0';
+       *pass = '\0';
+       sscanf(req->idAuthentication->u.open, "%63[^/]/%63s", user, pass);
+       ret = cfg->check_authentication(user, 0, pass);
+    }
+    else
+       ret = cfg->check_authentication(0, 0, 0);
+    return ret;
+}
+
 Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu)
 {
     m_marcxml_flag = 0;
@@ -2563,6 +2599,17 @@ void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu)
        }
        m_client->m_init_flag = 1;
     }
+    
+    if (!handle_authentication(apdu))
+    {
+       Z_APDU *apdu_reject = zget_APDU(odr_encode(), Z_APDU_initResponse);
+       *apdu_reject->u.initResponse->result = 0;
+       send_to_client(apdu_reject);
+
+       shutdown();
+       return;
+    }
+
     handle_max_record_retrieve(apdu);
 
     if (apdu)