X-Git-Url: http://git.indexdata.com/?p=yazproxy-moved-to-github.git;a=blobdiff_plain;f=src%2Fyaz-proxy.cpp;h=2d418a101e440d892b61ad21f99ba987b4cffde3;hp=e97946a12597b633f263d5c45d240a374a8a1158;hb=e88de60f087ada19ac4cc76e6b00b3b518984346;hpb=d6baec0e7369dd5d01a2998efb3667e3deff6db2 diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index e97946a..2d418a1 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -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.24 2005-02-22 10:08:20 adam Exp $ Copyright (c) 1998-2005, Index Data. This file is part of the yaz-proxy. @@ -38,6 +38,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #endif #include +#include #include #include @@ -116,7 +117,6 @@ Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable, m_keepalive_limit_pdu = 1000; m_proxyTarget = 0; m_default_target = 0; - m_proxy_authentication = 0; m_proxy_negotiation_charset = 0; m_proxy_negotiation_lang = 0; m_max_clients = 150; @@ -140,7 +140,7 @@ Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable, m_referenceId = 0; m_referenceId_mem = nmem_create(); m_config = 0; - m_marcxml_flag = 0; + m_marcxml_mode = none; m_stylesheet_xsp = 0; m_stylesheet_nprl = 0; m_s2z_stylesheet = 0; @@ -196,7 +196,6 @@ Yaz_Proxy::~Yaz_Proxy() xfree(m_proxyTarget); xfree(m_default_target); - xfree(m_proxy_authentication); xfree(m_proxy_negotiation_charset); xfree(m_proxy_negotiation_lang); xfree(m_optimize); @@ -222,6 +221,11 @@ Yaz_Proxy::~Yaz_Proxy() delete m_config; } +void Yaz_Proxy::set_debug_mode(int mode) +{ + m_debug_mode = mode; +} + int Yaz_Proxy::set_config(const char *config) { delete m_config; @@ -242,18 +246,11 @@ void Yaz_Proxy::set_default_target(const char *target) m_default_target = (char *) xstrdup (target); } -void Yaz_Proxy::set_proxy_authentication (const char *auth) -{ - xfree (m_proxy_authentication); - m_proxy_authentication = 0; - 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 +259,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) @@ -308,7 +306,6 @@ IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable new_proxy->set_APDU_yazlog(1); else new_proxy->set_APDU_yazlog(0); - new_proxy->set_proxy_authentication(m_proxy_authentication); new_proxy->set_proxy_negotiation(m_proxy_negotiation_charset, m_proxy_negotiation_lang); sprintf(new_proxy->m_session_str, "%ld:%d ", (long) time(0), m_session_no); @@ -413,18 +410,18 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie, Yaz_ProxyConfig *cfg = check_reconfigure(); if (proxy_host) { -#if 0 -/* only to be enabled for debugging... */ - if (!strcmp(proxy_host, "stop")) - exit(0); -#endif + if (parent && parent->m_debug_mode) + { + // only to be enabled for debugging... + if (!strcmp(proxy_host, "stop")) + exit(0); + } xfree(m_default_target); m_default_target = xstrdup(proxy_host); } proxy_host = m_default_target; int client_idletime = -1; const char *cql2rpn_fname = 0; - const char *authentication = 0; const char *negotiation_charset = 0; const char *negotiation_lang = 0; url[0] = m_default_target; @@ -440,7 +437,6 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie, &m_keepalive_limit_pdu, &pre_init, &cql2rpn_fname, - &authentication, &negotiation_charset, &negotiation_lang); } @@ -451,11 +447,12 @@ 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 (negotiation_charset || negotiation_lang) + { + yaz_log(YLOG_LOG, "set_proxy_negotiation..."); set_proxy_negotiation(negotiation_charset, negotiation_lang); + } if (!url[0]) { yaz_log(YLOG_LOG, "%sNo default target", m_session_str); @@ -559,23 +556,9 @@ Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu, const char *cookie, } Z_InitRequest *initRequest = apdu->u.initRequest; - if (!initRequest->idAuthentication) - { - if (m_proxy_authentication) - { - initRequest->idAuthentication = - (Z_IdAuthentication *) - odr_malloc (odr_encode(), - sizeof(*initRequest->idAuthentication)); - initRequest->idAuthentication->which = - Z_IdAuthentication_open; - initRequest->idAuthentication->u.open = - odr_strdup (odr_encode(), m_proxy_authentication); - } - } - else + if (initRequest->idAuthentication) { - // the client use authentication. We set the keepalive PDU + // the client uses authentication. We set the keepalive PDU // to 0 so we don't cache it in releaseClient m_keepalive_limit_pdu = 0; } @@ -842,6 +825,7 @@ void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p) continue; } #endif +/* HAVE_USEMARCON */ npr->u.databaseRecord = z_ext_record(odr_encode(), m_frontend_type, @@ -869,7 +853,18 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p, if (npr->which == Z_NamePlusRecord_databaseRecord) { Z_External *r = npr->u.databaseRecord; - if (r->which == Z_External_octet) + if (r->which == Z_External_OPAC) + { + WRBUF w = wrbuf_alloc(); + + yaz_display_OPAC(w, r->u.opac, 0); + npr->u.databaseRecord = z_ext_record( + odr_encode(), VAL_TEXT_XML, + wrbuf_buf(w), wrbuf_len(w) + ); + wrbuf_free(w, 1); + } + else if (r->which == Z_External_octet) { int rlen; char *result; @@ -877,9 +872,8 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p, r->u.octet_aligned->len, &result, &rlen)) { - npr->u.databaseRecord = z_ext_record(odr_encode(), - VAL_TEXT_XML, - result, rlen); + npr->u.databaseRecord = + z_ext_record(odr_encode(), VAL_TEXT_XML, result, rlen); } } } @@ -1198,7 +1192,7 @@ int Yaz_Proxy::send_to_client(Z_APDU *apdu) #endif ) convert_to_frontend_type(p->u.databaseOrSurDiagnostics); - if (m_marcxml_flag) + if (m_marcxml_mode == marcxml) convert_to_marcxml(p->u.databaseOrSurDiagnostics, m_backend_charset); if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu)) @@ -1243,7 +1237,7 @@ int Yaz_Proxy::send_to_client(Z_APDU *apdu) #endif ) convert_to_frontend_type(p->u.databaseOrSurDiagnostics); - if (m_marcxml_flag) + if (m_marcxml_mode == marcxml) convert_to_marcxml(p->u.databaseOrSurDiagnostics, m_backend_charset); if (convert_xsl(p->u.databaseOrSurDiagnostics, apdu)) @@ -1667,7 +1661,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,9 +1829,47 @@ 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->client_authentication(m_default_target, 0, 0, 0); + } + else if (req->idAuthentication->which == Z_IdAuthentication_idPass) + { + ret = cfg->client_authentication(m_default_target, + 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->client_authentication(m_default_target, user, 0, pass); + } + else + ret = cfg->client_authentication(m_default_target, 0, 0, 0); + + cfg->target_authentication(m_default_target, odr_encode(), req); + + return ret; +} + Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) { - m_marcxml_flag = 0; + m_marcxml_mode = none; if (apdu->which == Z_APDU_searchRequest) { Z_SearchRequest *sr = apdu->u.searchRequest; @@ -1891,6 +1922,7 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) { sr->smallSetElementSetNames = 0; sr->mediumSetElementSetNames = 0; + m_marcxml_mode = marcxml; if (m_backend_type) { @@ -1902,7 +1934,6 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) sr->preferredRecordSyntax = yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC); - m_marcxml_flag = 1; } else if (err) { @@ -1967,6 +1998,7 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) if (err == -1) { pr->recordComposition = 0; + m_marcxml_mode = marcxml; if (m_backend_type) { @@ -1978,7 +2010,6 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) pr->preferredRecordSyntax = yaz_oidval_to_z3950oid(odr_encode(), CLASS_RECSYN, VAL_USMARC); - m_marcxml_flag = 1; } else if (err) { @@ -2563,6 +2594,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)