X-Git-Url: http://git.indexdata.com/?p=yazproxy-moved-to-github.git;a=blobdiff_plain;f=src%2Fyaz-proxy.cpp;h=258c5b421a6cdbaea88b729ea2d06b10bcbd7b94;hp=194b9736ffeab35eddcedfcb43a505313545f3e7;hb=5076e32b1de695dc3e7b9509c9b05104b01a5f90;hpb=9f38fa901bed3a1bcc18581520aeb5a7e29ff630 diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index 194b973..258c5b4 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -1,5 +1,5 @@ -/* $Id: yaz-proxy.cpp,v 1.68 2006-06-09 09:35:14 adam Exp $ - Copyright (c) 1998-2006, Index Data. +/* $Id: yaz-proxy.cpp,v 1.78 2008-02-21 09:33:23 adam Exp $ + Copyright (c) 1998-2007, Index Data. This file is part of the yazproxy. @@ -51,6 +51,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include +#include #include "msg-thread.h" using namespace yazpp_1; @@ -250,7 +251,7 @@ Yaz_Proxy::Yaz_Proxy(IPDU_Observable *the_PDU_Observable, m_schema = 0; m_backend_type = 0; m_backend_charset = 0; - m_frontend_type = 0; + m_frontend_type[0] = -1; m_initRequest_apdu = 0; m_initRequest_mem = 0; m_initRequest_preferredMessageSize = 0; @@ -314,7 +315,7 @@ Yaz_Proxy::~Yaz_Proxy() delete m_charset_converter; xfree(m_optimize); -#if HAVE_XSLT +#if YAZ_HAVE_XSLT if (m_stylesheet_xsp) xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp); #endif @@ -398,7 +399,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); @@ -483,31 +483,20 @@ IPDU_Observer *Yaz_Proxy::sessionNotify(IPDU_Observable char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo) { - int oid[OID_SIZE]; - Z_OtherInformationUnit *oi; - struct oident ent; - ent.proto = PROTO_Z3950; - ent.oclass = CLASS_USERINFO; - ent.value = (oid_value) VAL_COOKIE; - assert (oid_ent_to_oid (&ent, oid)); - - if (oid_ent_to_oid (&ent, oid) && - (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) && - oi->which == Z_OtherInfo_characterInfo) + Z_OtherInformationUnit *oi = + update_otherInformation(otherInfo, 0, yaz_oid_userinfo_cookie, 1, 1); + + if (oi && oi->which == Z_OtherInfo_characterInfo) return oi->information.characterInfo; return 0; } + char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo) { - int oid[OID_SIZE]; - Z_OtherInformationUnit *oi; - struct oident ent; - ent.proto = PROTO_Z3950; - ent.oclass = CLASS_USERINFO; - ent.value = (oid_value) VAL_PROXY; - if (oid_ent_to_oid (&ent, oid) && - (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) && - oi->which == Z_OtherInfo_characterInfo) + Z_OtherInformationUnit *oi = + update_otherInformation(otherInfo, 0, yaz_oid_userinfo_proxy, 1, 1); + + if (oi && oi->which == Z_OtherInfo_characterInfo) return oi->information.characterInfo; return 0; } @@ -828,7 +817,6 @@ void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num) int i; for (i = 0; iwhich != Z_DiagRec_defaultFormat) @@ -838,9 +826,6 @@ void Yaz_Proxy::display_diagrecs(Z_DiagRec **pp, int num) } else r = p->u.defaultFormat; - if (!(ent = oid_getentbyoid(r->diagnosticSetId)) || - ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1) - yaz_log(YLOG_LOG, "%sError unknown diagnostic set", m_session_str); switch (r->which) { case Z_DefaultDiagFormat_v2Addinfo: @@ -877,7 +862,7 @@ int Yaz_Proxy::convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu) void Yaz_Proxy::convert_xsl_delay() { -#if HAVE_XSLT +#if YAZ_HAVE_XSLT Z_NamePlusRecord *npr = m_stylesheet_nprl->records[m_stylesheet_offset]; if (npr->which == Z_NamePlusRecord_databaseRecord) { @@ -902,11 +887,10 @@ void Yaz_Proxy::convert_xsl_delay() xmlChar *out_buf; int out_len; xmlDocDumpFormatMemory (res, &out_buf, &out_len, 1); - m_stylesheet_nprl->records[m_stylesheet_offset]-> u.databaseRecord = - z_ext_record(odr_encode(), VAL_TEXT_XML, - (char*) out_buf, out_len); + z_ext_record_oid(odr_encode(), yaz_oid_recsyn_xml, + (char*) out_buf, out_len); xmlFree(out_buf); xmlFreeDoc(res); } @@ -920,7 +904,7 @@ void Yaz_Proxy::convert_xsl_delay() { m_timeout_mode = timeout_normal; m_stylesheet_nprl = 0; -#if HAVE_XSLT +#if YAZ_HAVE_XSLT if (m_stylesheet_xsp) xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp); #endif @@ -934,7 +918,7 @@ void Yaz_Proxy::convert_xsl_delay() void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p) { - if (m_frontend_type != VAL_NONE) + if (m_frontend_type[0] != -1) { int i; for (i = 0; i < p->num_records; i++) @@ -980,10 +964,10 @@ void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p) } } npr->u.databaseRecord = - z_ext_record(odr_encode(), - m_frontend_type, - converted, - strlen(converted)); + z_ext_record_oid(odr_encode(), + m_frontend_type, + converted, + strlen(converted)); free(converted); } else @@ -995,10 +979,10 @@ void Yaz_Proxy::convert_to_frontend_type(Z_NamePlusRecordList *p) #endif /* HAVE_USEMARCON */ npr->u.databaseRecord = - z_ext_record(odr_encode(), - m_frontend_type, - (char*) r->u.octet_aligned->buf, - r->u.octet_aligned->len); + z_ext_record_oid(odr_encode(), + m_frontend_type, + (char*) r->u.octet_aligned->buf, + r->u.octet_aligned->len); } } } @@ -1026,36 +1010,36 @@ void Yaz_Proxy::convert_records_charset(Z_NamePlusRecordList *p, if (npr->which == Z_NamePlusRecord_databaseRecord) { Z_External *r = npr->u.databaseRecord; - oident *ent = oid_getentbyoid(r->direct_reference); - if (!ent || ent->value == VAL_NONE) + const Odr_oid *oid = r->direct_reference; + if (!oid) continue; - if (ent->value == VAL_SUTRS) + if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs)) { WRBUF w = wrbuf_alloc(); wrbuf_iconv_write(w, cd, (char*) r->u.octet_aligned->buf, r->u.octet_aligned->len); npr->u.databaseRecord = - z_ext_record(odr_encode(), ent->value, wrbuf_buf(w), - wrbuf_len(w)); - wrbuf_free(w, 1); + z_ext_record_oid(odr_encode(), oid, wrbuf_buf(w), + wrbuf_len(w)); + wrbuf_destroy(w); } - else if (ent->value == VAL_TEXT_XML) + else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml)) { ; } else if (r->which == Z_External_octet) { - int rlen; - char *result; + size_t rlen; + const char *result; if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf, r->u.octet_aligned->len, &result, &rlen)) { npr->u.databaseRecord = - z_ext_record(odr_encode(), ent->value, result, rlen); + z_ext_record_oid(odr_encode(), oid, result, rlen); yaz_log(YLOG_LOG, "%sRecoding MARC record", m_session_str); } @@ -1089,22 +1073,22 @@ void Yaz_Proxy::convert_to_marcxml(Z_NamePlusRecordList *p, WRBUF w = wrbuf_alloc(); yaz_opac_decode_wrbuf(mt, r->u.opac, w); - npr->u.databaseRecord = z_ext_record( - odr_encode(), VAL_TEXT_XML, - wrbuf_buf(w), wrbuf_len(w) - ); - wrbuf_free(w, 1); + npr->u.databaseRecord = z_ext_record_oid( + odr_encode(), yaz_oid_recsyn_xml, + wrbuf_buf(w), wrbuf_len(w)); + wrbuf_destroy(w); } else if (r->which == Z_External_octet) { - int rlen; - char *result; + size_t rlen; + const char *result; if (yaz_marc_decode_buf(mt, (char*) r->u.octet_aligned->buf, r->u.octet_aligned->len, &result, &rlen)) { npr->u.databaseRecord = - z_ext_record(odr_encode(), VAL_TEXT_XML, result, rlen); + z_ext_record_oid(odr_encode(), yaz_oid_recsyn_xml, + result, rlen); } } } @@ -1144,6 +1128,10 @@ int Yaz_Proxy::send_http_response(int code) z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive"); else timeout(0); + if (code == 401) + z_HTTP_header_add(o, &hres->headers, "WWW-Authenticate", + "Basic realm=\"YAZ Proxy\""); + if (m_log_mask & PROXY_LOG_REQ_CLIENT) { @@ -1178,7 +1166,7 @@ int Yaz_Proxy::send_srw_response(Z_SRW_PDU *srw_pdu, int http_code /* = 200 */) z_HTTP_header_add(o, &hres->headers, "WWW-Authenticate", "Basic realm=\"YAZ Proxy\""); static Z_SOAP_Handler soap_handlers[2] = { -#if HAVE_XSLT +#if YAZ_HAVE_XSLT {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec}, #endif @@ -1264,8 +1252,9 @@ int Yaz_Proxy::send_to_srw_client_ok(int hits, Z_Records *records, int start) continue; } Z_External *r = npr->u.databaseRecord; - oident *ent = oid_getentbyoid(r->direct_reference); - if (r->which == Z_External_octet && ent->value == VAL_TEXT_XML) + + if (r->which == Z_External_octet + && !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml)) { srw_res->records[i].recordSchema = m_schema; srw_res->records[i].recordPacking = m_s2z_packing; @@ -1714,7 +1703,7 @@ Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu) return 0; } - char query_str[120]; + char query_str[4096]; this_query->print(query_str, sizeof(query_str)-1); yaz_log(YLOG_LOG, "%sSearch %s", m_session_str, query_str); @@ -2277,8 +2266,7 @@ Z_Records *Yaz_Proxy::create_nonSurrogateDiagnostics(ODR odr, *err = error; rec->which = Z_Records_NSD; rec->u.nonSurrogateDiagnostic = dr; - dr->diagnosticSetId = - yaz_oidval_to_z3950oid (odr, CLASS_DIAGSET, VAL_BIB1); + dr->diagnosticSetId = odr_oiddup(odr, yaz_oid_diagset_bib_1); dr->condition = err; dr->which = Z_DefaultDiagFormat_v2Addinfo; dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : ""); @@ -2417,6 +2405,44 @@ int Yaz_Proxy::handle_authentication(Z_APDU *apdu) return ret; } +int Yaz_Proxy::handle_global_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->global_client_authentication(0, 0, 0, + m_peername); + } + else if (req->idAuthentication->which == Z_IdAuthentication_idPass) + { + ret = cfg->global_client_authentication( + req->idAuthentication->u.idPass->userId, + req->idAuthentication->u.idPass->groupId, + req->idAuthentication->u.idPass->password, + m_peername); + } + 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->global_client_authentication(user, 0, pass, + m_peername); + } + else + ret = cfg->global_client_authentication(0, 0, 0, m_peername); + return ret; +} + Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) { m_marcxml_mode = none; @@ -2436,13 +2462,9 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) } if (sr->preferredRecordSyntax) - { - struct oident *ent; - ent = oid_getentbyoid(sr->preferredRecordSyntax); - m_frontend_type = ent->value; - } + oid_oidcpy(m_frontend_type, sr->preferredRecordSyntax); else - m_frontend_type = VAL_NONE; + m_frontend_type[0] = -1; char *stylesheet_name = 0; if (cfg) @@ -2457,7 +2479,7 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) { m_parent->low_socket_close(); -#if HAVE_XSLT +#if YAZ_HAVE_XSLT if (m_stylesheet_xsp) xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp); m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*) @@ -2473,17 +2495,11 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) sr->smallSetElementSetNames = 0; sr->mediumSetElementSetNames = 0; m_marcxml_mode = marcxml; - 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); + sr->preferredRecordSyntax = + yaz_string_to_oid_odr( + yaz_oid_std(), CLASS_RECSYN, + m_backend_type ? m_backend_type : "usmarc", + odr_encode()); } else if (err) { @@ -2501,7 +2517,8 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) else if (m_backend_type) { sr->preferredRecordSyntax = - yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type); + yaz_string_to_oid_odr(yaz_oid_std(), CLASS_RECSYN, + m_backend_type, odr_encode()); } } else if (apdu->which == Z_APDU_presentRequest) @@ -2512,13 +2529,9 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) Yaz_ProxyConfig *cfg = check_reconfigure(); if (pr->preferredRecordSyntax) - { - struct oident *ent; - ent = oid_getentbyoid(pr->preferredRecordSyntax); - m_frontend_type = ent->value; - } + oid_oidcpy(m_frontend_type, pr->preferredRecordSyntax); else - m_frontend_type = VAL_NONE; + m_frontend_type[0] = -1; char *stylesheet_name = 0; if (cfg) @@ -2534,7 +2547,7 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) { m_parent->low_socket_close(); -#if HAVE_XSLT +#if YAZ_HAVE_XSLT if (m_stylesheet_xsp) xsltFreeStylesheet((xsltStylesheetPtr) m_stylesheet_xsp); m_stylesheet_xsp = xsltParseStylesheetFile((const xmlChar*) @@ -2549,17 +2562,12 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) { pr->recordComposition = 0; m_marcxml_mode = marcxml; - 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); + pr->preferredRecordSyntax = + yaz_string_to_oid_odr( + yaz_oid_std(), CLASS_RECSYN, + m_backend_type ? m_backend_type : "usmarc", + odr_encode()); } else if (err) { @@ -2578,7 +2586,9 @@ Z_APDU *Yaz_Proxy::handle_syntax_validation(Z_APDU *apdu) else if (m_backend_type) { pr->preferredRecordSyntax = - yaz_str_to_z3950oid(odr_encode(), CLASS_RECSYN, m_backend_type); + yaz_string_to_oid_odr(yaz_oid_std(), + CLASS_RECSYN, m_backend_type, + odr_encode()); } } return apdu; @@ -2754,24 +2764,51 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) m_s2z_present_apdu = 0; m_s2z_stylesheet = 0; - + Z_IdAuthentication *auth = NULL; - if (*authorization_str) + if (srw_pdu->username && srw_pdu->password) { + yaz_log(YLOG_LOG, "username/password: %s/%s\n", + srw_pdu->username, srw_pdu->password); 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; - char *p = strchr(authorization_str, ':'); - if (p) + auth->u.idPass->password = odr_strdup(m_s2z_odr_init, srw_pdu->password); + auth->u.idPass->userId = odr_strdup(m_s2z_odr_init, srw_pdu->username); + } + else + { + if (*authorization_str) { - *p = '\0'; - p++; - auth->u.idPass->password = odr_strdup(m_s2z_odr_init, p); + yaz_log(YLOG_LOG, "authorization_str present: %s\n", authorization_str); + 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; + char *p = strchr(authorization_str, ':'); + if (p) + { + *p = '\0'; + p++; + auth->u.idPass->password = odr_strdup(m_s2z_odr_init, p); + } + auth->u.idPass->userId = odr_strdup(m_s2z_odr_init, authorization_str); } - 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) { @@ -2902,8 +2939,8 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) *z_searchRequest->largeSetLowerBound = 2000000000; // 2e9 z_searchRequest->preferredRecordSyntax = - yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN, - VAL_TEXT_XML); + odr_oiddup(m_s2z_odr_search, yaz_oid_recsyn_xml); + if (srw_req->recordSchema) { z_searchRequest->smallSetElementSetNames = @@ -2920,9 +2957,9 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) m_s2z_present_apdu->u.presentRequest; *z_presentRequest->resultSetStartPoint = start; *z_presentRequest->numberOfRecordsRequested = max; + z_presentRequest->preferredRecordSyntax = - yaz_oidval_to_z3950oid(m_s2z_odr_search, CLASS_RECSYN, - VAL_TEXT_XML); + odr_oiddup(m_s2z_odr_search, yaz_oid_recsyn_xml); if (srw_req->recordSchema) { z_presentRequest->recordComposition = @@ -3127,8 +3164,8 @@ void Yaz_Proxy::handle_init(Z_APDU *apdu) Z_APDU *apdu2 = m_client->m_initResponse; apdu2->u.initResponse->otherInfo = 0; if (m_client->m_cookie && *m_client->m_cookie) - set_otherInformationString(apdu2, VAL_COOKIE, 1, - m_client->m_cookie); + set_otherInformationString(apdu2, yaz_oid_userinfo_cookie, + 1, m_client->m_cookie); apdu2->u.initResponse->referenceId = apdu->u.initRequest->referenceId; apdu2->u.initResponse->options = m_client->m_initResponse_options; @@ -3192,6 +3229,23 @@ void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu) if (apdu->which == Z_APDU_searchRequest) m_search_stat.add_bytes(1); + // Handle global authentication + if (!handle_global_authentication(apdu)) + { + if (m_http_version) + { // HTTP. Send unauthorized + send_http_response(401); + return; + } + else + { + // Z39.50 just shutdown + timeout(0); + return; + } + return; + } + // Determine our client. Z_OtherInformation **oi; get_otherInfoAPDU(apdu, &oi); @@ -3775,7 +3829,7 @@ void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu, int len) } } if (m_cookie) - set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie); + set_otherInformationString(apdu, yaz_oid_userinfo_cookie, 1, m_cookie); Yaz_Proxy *server = m_server; // save it. send_to_client may destroy us