Torus setting authentiation signals auth type
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include "filter_zoom.hpp"
24 #include <metaproxy/package.hpp>
25 #include <metaproxy/util.hpp>
26 #include <metaproxy/xmlutil.hpp>
27 #include <yaz/comstack.h>
28 #include <yaz/poll.h>
29 #include "torus.hpp"
30
31 #include <libxslt/xsltutils.h>
32 #include <libxslt/transform.h>
33
34 #include <boost/thread/mutex.hpp>
35 #include <boost/thread/condition.hpp>
36
37 #include <yaz/yaz-version.h>
38 #include <yaz/tpath.h>
39 #include <yaz/srw.h>
40 #include <yaz/ccl_xml.h>
41 #include <yaz/ccl.h>
42 #include <yaz/rpn2cql.h>
43 #include <yaz/rpn2solr.h>
44 #include <yaz/pquery.h>
45 #include <yaz/cql.h>
46 #include <yaz/oid_db.h>
47 #include <yaz/diagbib1.h>
48 #include <yaz/log.h>
49 #include <yaz/zgdu.h>
50 #include <yaz/querytowrbuf.h>
51 #include <yaz/sortspec.h>
52 #include <yaz/tokenizer.h>
53 #include <yaz/zoom.h>
54
55 namespace mp = metaproxy_1;
56 namespace yf = mp::filter;
57
58 namespace metaproxy_1 {
59     namespace filter {
60         class Zoom::Searchable : boost::noncopyable {
61           public:
62             std::string authentication;
63             std::string cfAuth;
64             std::string cfProxy;
65             std::string cfSubDB;
66             std::string udb;
67             std::string target;
68             std::string query_encoding;
69             std::string sru;
70             std::string sru_version;
71             std::string request_syntax;
72             std::string element_set;
73             std::string record_encoding;
74             std::string transform_xsl_fname;
75             std::string transform_xsl_content;
76             std::string urlRecipe;
77             std::string contentConnector;
78             std::string sortStrategy;
79             std::string extraArgs;
80             std::string rpn2cql_fname;
81             bool use_turbomarc;
82             bool piggyback;
83             CCL_bibset ccl_bibset;
84             std::map<std::string, std::string> sortmap;
85             Searchable(CCL_bibset base);
86             ~Searchable();
87         };
88         class Zoom::Backend : boost::noncopyable {
89             friend class Impl;
90             friend class Frontend;
91             std::string zurl;
92             mp::wrbuf m_apdu_wrbuf;
93             ZOOM_connection m_connection;
94             ZOOM_resultset m_resultset;
95             std::string m_frontend_database;
96             SearchablePtr sptr;
97             xsltStylesheetPtr xsp;
98             std::string cproxy_host;
99             bool enable_cproxy;
100             bool enable_explain;
101             xmlDoc *explain_doc;
102             std::string m_proxy;
103             cql_transform_t cqlt;
104         public:
105             Backend();
106             ~Backend();
107             void connect(std::string zurl, int *error, char **addinfo,
108                          ODR odr);
109             void search(ZOOM_query q, Odr_int *hits,
110                         int *error, char **addinfo, ODR odr);
111             void present(Odr_int start, Odr_int number, ZOOM_record *recs,
112                          int *error, char **addinfo, ODR odr);
113             void set_option(const char *name, const char *value);
114             void set_option(const char *name, std::string value);
115             const char *get_option(const char *name);
116             void get_zoom_error(int *error, char **addinfo, ODR odr);
117         };
118         class Zoom::Frontend : boost::noncopyable {
119             friend class Impl;
120             Impl *m_p;
121             bool m_is_virtual;
122             bool m_in_use;
123             std::string session_realm;
124             yazpp_1::GDU m_init_gdu;
125             BackendPtr m_backend;
126             void handle_package(mp::Package &package);
127             void handle_search(mp::Package &package);
128
129             void auth(mp::Package &package, Z_InitRequest *req,
130                       int *error, char **addinfo, ODR odr);
131
132             BackendPtr explain_search(mp::Package &package,
133                                       std::string &database,
134                                       int *error,
135                                       char **addinfo,
136                                       mp::odr &odr,
137                                       std::string torus_url,
138                                       std::string &torus_db,
139                                       std::string &realm);
140             void handle_present(mp::Package &package);
141             BackendPtr get_backend_from_databases(mp::Package &package,
142                                                   std::string &database,
143                                                   int *error,
144                                                   char **addinfo,
145                                                   mp::odr &odr,
146                                                   int *proxy_step);
147
148             bool create_content_session(mp::Package &package,
149                                         BackendPtr b,
150                                         int *error,
151                                         char **addinfo,
152                                         ODR odr,
153                                         std::string authentication,
154                                         std::string proxy,
155                                         std::string realm);
156
157             void prepare_elements(BackendPtr b,
158                                   Odr_oid *preferredRecordSyntax,
159                                   const char *element_set_name,
160                                   bool &enable_pz2_retrieval,
161                                   bool &enable_pz2_transform,
162                                   bool &enable_record_transform,
163                                   bool &assume_marc8_charset);
164
165             Z_Records *get_records(Package &package,
166                                    Odr_int start,
167                                    Odr_int number_to_present,
168                                    int *error,
169                                    char **addinfo,
170                                    Odr_int *number_of_records_returned,
171                                    ODR odr, BackendPtr b,
172                                    Odr_oid *preferredRecordSyntax,
173                                    const char *element_set_name);
174             Z_Records *get_explain_records(Package &package,
175                                            Odr_int start,
176                                            Odr_int number_to_present,
177                                            int *error,
178                                            char **addinfo,
179                                            Odr_int *number_of_records_returned,
180                                            ODR odr, BackendPtr b,
181                                            Odr_oid *preferredRecordSyntax,
182                                            const char *element_set_name);
183             bool retry(mp::Package &package,
184                        mp::odr &odr,
185                        BackendPtr b,
186                        int &error, char **addinfo,
187                        int &proxy_step, int &same_retries,
188                        int &proxy_retries);
189             void log_diagnostic(mp::Package &package,
190                                 int error, const char *addinfo);
191         public:
192             Frontend(Impl *impl);
193             ~Frontend();
194         };
195         class Zoom::Impl {
196             friend class Frontend;
197         public:
198             Impl();
199             ~Impl();
200             void process(metaproxy_1::Package & package);
201             void configure(const xmlNode * ptr, bool test_only,
202                            const char *path);
203         private:
204             void configure_local_records(const xmlNode * ptr, bool test_only);
205             bool check_proxy(const char *proxy);
206
207
208
209             FrontendPtr get_frontend(mp::Package &package);
210             void release_frontend(mp::Package &package);
211             SearchablePtr parse_torus_record(const xmlNode *ptr);
212             struct cql_node *convert_cql_fields(struct cql_node *cn, ODR odr);
213             std::map<mp::Session, FrontendPtr> m_clients;
214             boost::mutex m_mutex;
215             boost::condition m_cond_session_ready;
216             std::string torus_searchable_url;
217             std::string torus_content_url;
218             std::string torus_auth_url;
219             std::string default_realm;
220             std::map<std::string,std::string> fieldmap;
221             std::string xsldir;
222             std::string file_path;
223             std::string content_proxy_server;
224             std::string content_tmp_file;
225             std::string content_config_file;
226             bool apdu_log;
227             CCL_bibset bibset;
228             std::string element_transform;
229             std::string element_raw;
230             std::string element_passthru;
231             std::string proxy;
232             xsltStylesheetPtr explain_xsp;
233             xsltStylesheetPtr record_xsp;
234             std::map<std::string,SearchablePtr> s_map;
235             std::string zoom_timeout;
236             int proxy_timeout;
237         };
238     }
239 }
240
241
242 static xmlNode *xml_node_search(xmlNode *ptr, int *num, int m)
243 {
244     while (ptr)
245     {
246         if (ptr->type == XML_ELEMENT_NODE &&
247             !strcmp((const char *) ptr->name, "recordData"))
248         {
249             (*num)++;
250             if (m == *num)
251                 return ptr;
252         }
253         else  // else: we don't want to find nested nodes
254         {
255             xmlNode *ret_node = xml_node_search(ptr->children, num, m);
256             if (ret_node)
257                 return ret_node;
258         }
259         ptr = ptr->next;
260     }
261     return 0;
262 }
263
264 // define Pimpl wrapper forwarding to Impl
265
266 yf::Zoom::Zoom() : m_p(new Impl)
267 {
268 }
269
270 yf::Zoom::~Zoom()
271 {  // must have a destructor because of boost::scoped_ptr
272 }
273
274 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only,
275                          const char *path)
276 {
277     m_p->configure(xmlnode, test_only, path);
278 }
279
280 void yf::Zoom::process(mp::Package &package) const
281 {
282     m_p->process(package);
283 }
284
285
286 // define Implementation stuff
287
288 yf::Zoom::Backend::Backend()
289 {
290     m_connection = ZOOM_connection_create(0);
291     ZOOM_connection_save_apdu_wrbuf(m_connection, m_apdu_wrbuf);
292     m_resultset = 0;
293     xsp = 0;
294     enable_cproxy = true;
295     enable_explain = false;
296     explain_doc = 0;
297     cqlt = 0;
298 }
299
300 yf::Zoom::Backend::~Backend()
301 {
302     if (xsp)
303         xsltFreeStylesheet(xsp);
304     if (explain_doc)
305         xmlFreeDoc(explain_doc);
306     cql_transform_close(cqlt);
307     ZOOM_connection_destroy(m_connection);
308     ZOOM_resultset_destroy(m_resultset);
309 }
310
311
312 void yf::Zoom::Backend::get_zoom_error(int *error, char **addinfo,
313                                        ODR odr)
314 {
315     const char *msg = 0;
316     const char *zoom_addinfo = 0;
317     const char *dset = 0;
318     int error0 = ZOOM_connection_error_x(m_connection, &msg,
319                                          &zoom_addinfo, &dset);
320     if (error0)
321     {
322         if (!dset)
323             dset = "Unknown";
324
325         if (!strcmp(dset, "info:srw/diagnostic/1"))
326             *error = yaz_diag_srw_to_bib1(error0);
327         else if (!strcmp(dset, "Bib-1"))
328             *error = error0;
329         else if (!strcmp(dset, "ZOOM"))
330         {
331             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
332             if (error0 == ZOOM_ERROR_INIT)
333                 *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
334             else if (error0 == ZOOM_ERROR_DECODE)
335             {
336                 if (zoom_addinfo)
337                 {
338                     if (strstr(zoom_addinfo, "Authentication") ||
339                         strstr(zoom_addinfo, "authentication"))
340                         *error = YAZ_BIB1_INIT_AC_AUTHENTICATION_SYSTEM_ERROR;
341                 }
342             }
343         }
344         else
345             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
346
347         *addinfo = (char *) odr_malloc(
348             odr, 30 + strlen(dset) + strlen(msg) +
349             (zoom_addinfo ? strlen(zoom_addinfo) : 0));
350         **addinfo = '\0';
351         if (zoom_addinfo && *zoom_addinfo)
352         {
353             strcpy(*addinfo, zoom_addinfo);
354             strcat(*addinfo, " ");
355         }
356         sprintf(*addinfo + strlen(*addinfo), "(%s %d %s)", dset, error0, msg);
357     }
358 }
359
360 void yf::Zoom::Backend::connect(std::string zurl,
361                                 int *error, char **addinfo,
362                                 ODR odr)
363 {
364     size_t h = zurl.find_first_of('#');
365     if (h != std::string::npos)
366         zurl.erase(h);
367     ZOOM_connection_connect(m_connection, zurl.length() ? zurl.c_str() : 0, 0);
368     get_zoom_error(error, addinfo, odr);
369
370 }
371
372 void yf::Zoom::Backend::search(ZOOM_query q, Odr_int *hits,
373                                int *error, char **addinfo, ODR odr)
374 {
375     ZOOM_resultset_destroy(m_resultset);
376     m_resultset = ZOOM_connection_search(m_connection, q);
377     get_zoom_error(error, addinfo, odr);
378     if (*error == 0)
379         *hits = ZOOM_resultset_size(m_resultset);
380     else
381         *hits = 0;
382 }
383
384 void yf::Zoom::Backend::present(Odr_int start, Odr_int number,
385                                 ZOOM_record *recs,
386                                 int *error, char **addinfo, ODR odr)
387 {
388     ZOOM_resultset_records(m_resultset, recs, start, number);
389     get_zoom_error(error, addinfo, odr);
390 }
391
392 void yf::Zoom::Backend::set_option(const char *name, const char *value)
393 {
394     ZOOM_connection_option_set(m_connection, name, value);
395     if (m_resultset)
396         ZOOM_resultset_option_set(m_resultset, name, value);
397 }
398
399 void yf::Zoom::Backend::set_option(const char *name, std::string value)
400 {
401     set_option(name, value.c_str());
402 }
403
404 const char *yf::Zoom::Backend::get_option(const char *name)
405 {
406     return ZOOM_connection_option_get(m_connection, name);
407 }
408
409 yf::Zoom::Searchable::Searchable(CCL_bibset base)
410 {
411     piggyback = true;
412     use_turbomarc = true;
413     sortStrategy = "embed";
414     ccl_bibset = ccl_qual_dup(base);
415 }
416
417 yf::Zoom::Searchable::~Searchable()
418 {
419     ccl_qual_rm(&ccl_bibset);
420 }
421
422 yf::Zoom::Frontend::Frontend(Impl *impl) :
423     m_p(impl), m_is_virtual(false), m_in_use(true)
424 {
425 }
426
427 yf::Zoom::Frontend::~Frontend()
428 {
429 }
430
431 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
432 {
433     boost::mutex::scoped_lock lock(m_mutex);
434
435     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
436
437     while(true)
438     {
439         it = m_clients.find(package.session());
440         if (it == m_clients.end())
441             break;
442
443         if (!it->second->m_in_use)
444         {
445             it->second->m_in_use = true;
446             return it->second;
447         }
448         m_cond_session_ready.wait(lock);
449     }
450     FrontendPtr f(new Frontend(this));
451     m_clients[package.session()] = f;
452     f->m_in_use = true;
453     return f;
454 }
455
456 void yf::Zoom::Impl::release_frontend(mp::Package &package)
457 {
458     boost::mutex::scoped_lock lock(m_mutex);
459     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
460
461     it = m_clients.find(package.session());
462     if (it != m_clients.end())
463     {
464         if (package.session().is_closed())
465         {
466             m_clients.erase(it);
467         }
468         else
469         {
470             it->second->m_in_use = false;
471         }
472         m_cond_session_ready.notify_all();
473     }
474 }
475
476 yf::Zoom::Impl::Impl() :
477     apdu_log(false), element_transform("pz2") , element_raw("raw") ,
478     element_passthru("F"),
479     zoom_timeout("40"), proxy_timeout(1)
480 {
481     bibset = ccl_qual_mk();
482
483     explain_xsp = 0;
484     record_xsp = 0;
485     srand((unsigned int) time(0));
486 }
487
488 yf::Zoom::Impl::~Impl()
489 {
490     if (explain_xsp)
491         xsltFreeStylesheet(explain_xsp);
492     ccl_qual_rm(&bibset);
493 }
494
495 yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
496 {
497     Zoom::SearchablePtr s(new Searchable(bibset));
498
499     for (ptr = ptr->children; ptr; ptr = ptr->next)
500     {
501         if (ptr->type != XML_ELEMENT_NODE)
502             continue;
503         if (!strcmp((const char *) ptr->name, "layer"))
504             ptr = ptr->children;
505         else if (!strcmp((const char *) ptr->name,
506                          "authentication"))
507         {
508             s->authentication = mp::xml::get_text(ptr);
509         }
510         else if (!strcmp((const char *) ptr->name,
511                          "cfAuth"))
512         {
513             s->cfAuth = mp::xml::get_text(ptr);
514         }
515         else if (!strcmp((const char *) ptr->name,
516                          "cfProxy"))
517         {
518             s->cfProxy = mp::xml::get_text(ptr);
519         }
520         else if (!strcmp((const char *) ptr->name,
521                          "cfSubDB"))
522         {
523             s->cfSubDB = mp::xml::get_text(ptr);
524         }
525         else if (!strcmp((const char *) ptr->name,
526                          "contentConnector"))
527         {
528             s->contentConnector = mp::xml::get_text(ptr);
529         }
530         else if (!strcmp((const char *) ptr->name, "udb"))
531         {
532             s->udb = mp::xml::get_text(ptr);
533         }
534         else if (!strcmp((const char *) ptr->name, "zurl"))
535         {
536             s->target = mp::xml::get_text(ptr);
537         }
538         else if (!strcmp((const char *) ptr->name, "sru"))
539         {
540             s->sru = mp::xml::get_text(ptr);
541         }
542         else if (!strcmp((const char *) ptr->name, "SRUVersion") ||
543                  !strcmp((const char *) ptr->name, "sruVersion"))
544         {
545             s->sru_version = mp::xml::get_text(ptr);
546         }
547         else if (!strcmp((const char *) ptr->name,
548                          "queryEncoding"))
549         {
550             s->query_encoding = mp::xml::get_text(ptr);
551         }
552         else if (!strcmp((const char *) ptr->name,
553                          "piggyback"))
554         {
555             s->piggyback = mp::xml::get_bool(ptr, true);
556         }
557         else if (!strcmp((const char *) ptr->name,
558                          "requestSyntax"))
559         {
560             s->request_syntax = mp::xml::get_text(ptr);
561         }
562         else if (!strcmp((const char *) ptr->name,
563                          "elementSet"))
564         {
565             s->element_set = mp::xml::get_text(ptr);
566         }
567         else if (!strcmp((const char *) ptr->name,
568                          "recordEncoding"))
569         {
570             s->record_encoding = mp::xml::get_text(ptr);
571         }
572         else if (!strcmp((const char *) ptr->name,
573                          "transform"))
574         {
575             s->transform_xsl_fname = mp::xml::get_text(ptr);
576         }
577         else if (!strcmp((const char *) ptr->name,
578                          "literalTransform"))
579         {
580             s->transform_xsl_content = mp::xml::get_text(ptr);
581         }
582         else if (!strcmp((const char *) ptr->name,
583                          "urlRecipe"))
584         {
585             s->urlRecipe = mp::xml::get_text(ptr);
586         }
587         else if (!strcmp((const char *) ptr->name,
588                          "useTurboMarc"))
589         {
590             ; // useTurboMarc is ignored
591         }
592         else if (!strncmp((const char *) ptr->name,
593                           "cclmap_", 7))
594         {
595             std::string value = mp::xml::get_text(ptr);
596             if (value.length() > 0)
597             {
598                 ccl_qual_fitem(s->ccl_bibset, value.c_str(),
599                                (const char *) ptr->name + 7);
600             }
601         }
602         else if (!strncmp((const char *) ptr->name,
603                           "sortmap_", 8))
604         {
605             std::string value = mp::xml::get_text(ptr);
606             s->sortmap[(const char *) ptr->name + 8] = value;
607         }
608         else if (!strcmp((const char *) ptr->name,
609                           "sortStrategy"))
610         {
611             s->sortStrategy = mp::xml::get_text(ptr);
612         }
613         else if (!strcmp((const char *) ptr->name,
614                           "extraArgs"))
615         {
616             s->extraArgs = mp::xml::get_text(ptr);
617         }
618         else if (!strcmp((const char *) ptr->name, "rpn2cql"))
619             s->rpn2cql_fname = mp::xml::get_text(ptr);
620     }
621     return s;
622 }
623
624 void yf::Zoom::Impl::configure_local_records(const xmlNode *ptr, bool test_only)
625 {
626     while (ptr && ptr->type != XML_ELEMENT_NODE)
627         ptr = ptr->next;
628
629     if (ptr)
630     {
631         if (!strcmp((const char *) ptr->name, "records"))
632         {
633             for (ptr = ptr->children; ptr; ptr = ptr->next)
634             {
635                 if (ptr->type != XML_ELEMENT_NODE)
636                     continue;
637                 if (!strcmp((const char *) ptr->name, "record"))
638                 {
639                     SearchablePtr s = parse_torus_record(ptr);
640                     if (s)
641                     {
642                         std::string udb = s->udb;
643                         if (udb.length())
644                             s_map[s->udb] = s;
645                         else
646                         {
647                             throw mp::filter::FilterException
648                                 ("No udb for local torus record");
649                         }
650                     }
651                 }
652                 else
653                 {
654                     throw mp::filter::FilterException
655                         ("Bad element "
656                          + std::string((const char *) ptr->name)
657                          + " in zoom filter inside element "
658                          "<torus><records>");
659                 }
660             }
661         }
662         else
663         {
664             throw mp::filter::FilterException
665                 ("Bad element "
666                  + std::string((const char *) ptr->name)
667                  + " in zoom filter inside element <torus>");
668         }
669     }
670 }
671
672 void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only,
673                                const char *path)
674 {
675     std::string explain_xslt_fname;
676     std::string record_xslt_fname;
677
678     if (path && *path)
679     {
680         file_path = path;
681     }
682     for (ptr = ptr->children; ptr; ptr = ptr->next)
683     {
684         if (ptr->type != XML_ELEMENT_NODE)
685             continue;
686         else if (!strcmp((const char *) ptr->name, "torus"))
687         {
688             const struct _xmlAttr *attr;
689             for (attr = ptr->properties; attr; attr = attr->next)
690             {
691                 if (!strcmp((const char *) attr->name, "url"))
692                     torus_searchable_url = mp::xml::get_text(attr->children);
693                 else if (!strcmp((const char *) attr->name, "content_url"))
694                     torus_content_url = mp::xml::get_text(attr->children);
695                 else if (!strcmp((const char *) attr->name, "auth_url"))
696                     torus_auth_url = mp::xml::get_text(attr->children);
697                 else if (!strcmp((const char *) attr->name, "realm"))
698                     default_realm = mp::xml::get_text(attr->children);
699                 else if (!strcmp((const char *) attr->name, "xsldir"))
700                     xsldir = mp::xml::get_text(attr->children);
701                 else if (!strcmp((const char *) attr->name, "element_transform"))
702                     element_transform = mp::xml::get_text(attr->children);
703                 else if (!strcmp((const char *) attr->name, "element_raw"))
704                     element_raw = mp::xml::get_text(attr->children);
705                 else if (!strcmp((const char *) attr->name, "element_passthru"))
706                     element_passthru = mp::xml::get_text(attr->children);
707                 else if (!strcmp((const char *) attr->name, "proxy"))
708                     proxy = mp::xml::get_text(attr->children);
709                 else if (!strcmp((const char *) attr->name, "explain_xsl"))
710                     explain_xslt_fname = mp::xml::get_text(attr->children);
711                 else if (!strcmp((const char *) attr->name, "record_xsl"))
712                     record_xslt_fname = mp::xml::get_text(attr->children);
713                 else
714                     throw mp::filter::FilterException(
715                         "Bad attribute " + std::string((const char *)
716                                                        attr->name));
717             }
718             // If content_url is not given, use value of searchable, to
719             // ensure backwards compatibility
720             if (!torus_content_url.length())
721                 torus_content_url = torus_searchable_url;
722             configure_local_records(ptr->children, test_only);
723         }
724         else if (!strcmp((const char *) ptr->name, "cclmap"))
725         {
726             const char *addinfo = 0;
727             ccl_xml_config(bibset, ptr, &addinfo);
728         }
729         else if (!strcmp((const char *) ptr->name, "fieldmap"))
730         {
731             const struct _xmlAttr *attr;
732             std::string ccl_field;
733             std::string cql_field;
734             for (attr = ptr->properties; attr; attr = attr->next)
735             {
736                 if (!strcmp((const char *) attr->name, "ccl"))
737                     ccl_field = mp::xml::get_text(attr->children);
738                 else if (!strcmp((const char *) attr->name, "cql"))
739                     cql_field = mp::xml::get_text(attr->children);
740                 else
741                     throw mp::filter::FilterException(
742                         "Bad attribute " + std::string((const char *)
743                                                        attr->name));
744             }
745             if (cql_field.length())
746                 fieldmap[cql_field] = ccl_field;
747         }
748         else if (!strcmp((const char *) ptr->name, "contentProxy"))
749         {
750             const struct _xmlAttr *attr;
751             for (attr = ptr->properties; attr; attr = attr->next)
752             {
753                 if (!strcmp((const char *) attr->name, "server"))
754                 {
755                     yaz_log(YLOG_WARN,
756                             "contentProxy's server attribute is deprecated");
757                     yaz_log(YLOG_LOG,
758                             "Specify config_file instead. For example:");
759                     yaz_log(YLOG_LOG,
760                             " content_file=\"/etc/cf-proxy/cproxy.cfg\"");
761                     content_proxy_server = mp::xml::get_text(attr->children);
762                 }
763                 else if (!strcmp((const char *) attr->name, "tmp_file"))
764                     content_tmp_file = mp::xml::get_text(attr->children);
765                 else if (!strcmp((const char *) attr->name, "config_file"))
766                     content_config_file = mp::xml::get_text(attr->children);
767                 else
768                     throw mp::filter::FilterException(
769                         "Bad attribute " + std::string((const char *)
770                                                        attr->name));
771             }
772         }
773         else if (!strcmp((const char *) ptr->name, "log"))
774         {
775             const struct _xmlAttr *attr;
776             for (attr = ptr->properties; attr; attr = attr->next)
777             {
778                 if (!strcmp((const char *) attr->name, "apdu"))
779                     apdu_log = mp::xml::get_bool(attr->children, false);
780                 else
781                     throw mp::filter::FilterException(
782                         "Bad attribute " + std::string((const char *)
783                                                        attr->name));
784             }
785         }
786         else if (!strcmp((const char *) ptr->name, "zoom"))
787         {
788             const struct _xmlAttr *attr;
789             for (attr = ptr->properties; attr; attr = attr->next)
790             {
791                 if (!strcmp((const char *) attr->name, "timeout"))
792                     zoom_timeout = mp::xml::get_text(attr->children);
793                 else if (!strcmp((const char *) attr->name, "proxy_timeout"))
794                     proxy_timeout = mp::xml::get_int(attr->children, 1);
795                 else
796                     throw mp::filter::FilterException(
797                         "Bad attribute " + std::string((const char *)
798                                                        attr->name));
799             }
800         }
801         else
802         {
803             throw mp::filter::FilterException
804                 ("Bad element "
805                  + std::string((const char *) ptr->name)
806                  + " in zoom filter");
807         }
808     }
809
810     if (explain_xslt_fname.length())
811     {
812         const char *path = 0;
813
814         if (xsldir.length())
815             path = xsldir.c_str();
816         else
817             path = file_path.c_str();
818
819         char fullpath[1024];
820         char *cp = yaz_filepath_resolve(explain_xslt_fname.c_str(),
821                                         path, 0, fullpath);
822         if (!cp)
823         {
824             throw mp::filter::FilterException
825                 ("Cannot read XSLT " + explain_xslt_fname);
826         }
827
828         xmlDoc *xsp_doc = xmlParseFile(cp);
829         if (!xsp_doc)
830         {
831             throw mp::filter::FilterException
832                 ("Cannot parse XSLT " + explain_xslt_fname);
833         }
834
835         explain_xsp = xsltParseStylesheetDoc(xsp_doc);
836         if (!explain_xsp)
837         {
838             xmlFreeDoc(xsp_doc);
839             throw mp::filter::FilterException
840                 ("Cannot parse XSLT " + explain_xslt_fname);
841
842         }
843     }
844
845     if (record_xslt_fname.length())
846     {
847         const char *path = 0;
848
849         if (xsldir.length())
850             path = xsldir.c_str();
851         else
852             path = file_path.c_str();
853
854         char fullpath[1024];
855         char *cp = yaz_filepath_resolve(record_xslt_fname.c_str(),
856                                         path, 0, fullpath);
857         if (!cp)
858         {
859             throw mp::filter::FilterException
860                 ("Cannot read XSLT " + record_xslt_fname);
861         }
862
863         xmlDoc *xsp_doc = xmlParseFile(cp);
864         if (!xsp_doc)
865         {
866             throw mp::filter::FilterException
867                 ("Cannot parse XSLT " + record_xslt_fname);
868         }
869
870         record_xsp = xsltParseStylesheetDoc(xsp_doc);
871         if (!record_xsp)
872         {
873             xmlFreeDoc(xsp_doc);
874             throw mp::filter::FilterException
875                 ("Cannot parse XSLT " + record_xslt_fname);
876
877         }
878     }
879 }
880
881 bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
882                                                 BackendPtr b,
883                                                 int *error, char **addinfo,
884                                                 ODR odr,
885                                                 std::string authentication,
886                                                 std::string proxy,
887                                                 std::string realm)
888 {
889     if (b->sptr->contentConnector.length())
890     {
891         std::string proxyhostname;
892         std::string tmp_file;
893         bool legacy_format = false;
894
895         if (m_p->content_proxy_server.length())
896         {
897             proxyhostname = m_p->content_proxy_server;
898             legacy_format = true;
899         }
900
901         if (m_p->content_tmp_file.length())
902             tmp_file = m_p->content_tmp_file;
903
904         if (m_p->content_config_file.length())
905         {
906             FILE *inf = fopen(m_p->content_config_file.c_str(), "r");
907             if (inf)
908             {
909                 char buf[1024];
910                 while (fgets(buf, sizeof(buf)-1, inf))
911                 {
912                     char *cp;
913                     cp = strchr(buf, '#');
914                     if (cp)
915                         *cp = '\0';
916                     cp = strchr(buf, '\n');
917                     if (cp)
918                         *cp = '\0';
919                     cp = strchr(buf, ':');
920                     if (cp)
921                     {
922                         char *cp1 = cp;
923                         while (cp1 != buf && cp1[-1] == ' ')
924                             cp1--;
925                         *cp1 = '\0';
926                         cp++;
927                         while (*cp == ' ')
928                             cp++;
929                         if (!strcmp(buf, "proxyhostname"))
930                             proxyhostname = cp;
931                         if (!strcmp(buf, "sessiondir") && *cp)
932                         {
933                             if (cp[strlen(cp)-1] == '/')
934                                 cp[strlen(cp)-1] = '\0';
935                             tmp_file = std::string(cp) + std::string("/cf.XXXXXX.p");
936                         }
937                     }
938                 }
939                 fclose(inf);
940             }
941             else
942             {
943                 package.log("zoom", YLOG_WARN|YLOG_ERRNO,
944                             "unable to open content config %s",
945                             m_p->content_config_file.c_str());
946                 *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
947                 *addinfo = (char *)  odr_malloc(odr, 70 + tmp_file.length());
948                 sprintf(*addinfo, "zoom: unable to open content config %s",
949                         m_p->content_config_file.c_str());
950                 return false;
951             }
952         }
953
954         if (proxyhostname.length() == 0)
955         {
956             package.log("zoom", YLOG_WARN, "no proxyhostname");
957             return true;
958         }
959         if (tmp_file.length() == 0)
960         {
961             package.log("zoom", YLOG_WARN, "no tmp_file");
962             return true;
963         }
964
965         char *fname = xstrdup(tmp_file.c_str());
966         char *xx = strstr(fname, "XXXXXX");
967         if (!xx)
968         {
969             package.log("zoom", YLOG_WARN, "bad tmp_file %s", tmp_file.c_str());
970             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
971             *addinfo = (char *)  odr_malloc(odr, 60 + tmp_file.length());
972             sprintf(*addinfo, "zoom: bad format of content tmp_file: %s",
973                     tmp_file.c_str());
974             xfree(fname);
975             return false;
976         }
977         char tmp_char = xx[6];
978         sprintf(xx, "%06d", ((unsigned) rand()) % 1000000);
979         if (legacy_format)
980             b->cproxy_host = std::string(xx) + "." + proxyhostname;
981         else
982             b->cproxy_host = proxyhostname + "/" + xx;
983         xx[6] = tmp_char;
984
985         FILE *file = fopen(fname, "w");
986         if (!file)
987         {
988             package.log("zoom", YLOG_WARN|YLOG_ERRNO, "create %s", fname);
989             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
990             *addinfo = (char *) odr_malloc(odr, 50 + strlen(fname));
991             sprintf(*addinfo, "zoom: could not create %s", fname);
992             xfree(fname);
993             return false;
994         }
995         mp::wrbuf w;
996         wrbuf_puts(w, "#content_proxy\n");
997         wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str());
998         if (authentication.length())
999             wrbuf_printf(w, "auth: %s\n", authentication.c_str());
1000         if (proxy.length())
1001             wrbuf_printf(w, "proxy: %s\n", proxy.c_str());
1002         if (realm.length())
1003             wrbuf_printf(w, "realm: %s\n", realm.c_str());
1004
1005         fwrite(w.buf(), 1, w.len(), file);
1006         fclose(file);
1007         package.log("zoom", YLOG_LOG, "content file: %s", fname);
1008         xfree(fname);
1009     }
1010     return true;
1011 }
1012
1013 yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
1014     mp::Package &package,
1015     std::string &database, int *error, char **addinfo, mp::odr &odr,
1016     int *proxy_step)
1017 {
1018     bool connection_reuse = false;
1019     std::string proxy;
1020
1021     std::list<BackendPtr>::const_iterator map_it;
1022     if (m_backend && !m_backend->enable_explain &&
1023         m_backend->m_frontend_database == database)
1024     {
1025         connection_reuse = true;
1026         proxy = m_backend->m_proxy;
1027     }
1028
1029     std::string input_args;
1030     std::string torus_db;
1031     size_t db_arg_pos = database.find(',');
1032     if (db_arg_pos != std::string::npos)
1033     {
1034         torus_db = database.substr(0, db_arg_pos);
1035         input_args = database.substr(db_arg_pos + 1);
1036     }
1037     else
1038         torus_db = database;
1039
1040     std::string authentication;
1041     std::string content_authentication;
1042     std::string content_proxy;
1043     std::string realm = session_realm;
1044     if (realm.length() == 0)
1045         realm = m_p->default_realm;
1046
1047     const char *param_user = 0;
1048     const char *param_password = 0;
1049     const char *param_content_user = 0;
1050     const char *param_content_password = 0;
1051     const char *param_nocproxy = 0;
1052     int no_parms = 0;
1053
1054     char **names;
1055     char **values;
1056     int no_out_args = 0;
1057     if (input_args.length())
1058         no_parms = yaz_uri_to_array(input_args.c_str(),
1059                                     odr, &names, &values);
1060     // adding 10 because we'll be adding other URL args
1061     const char **out_names = (const char **)
1062         odr_malloc(odr, (10 + no_parms) * sizeof(*out_names));
1063     const char **out_values = (const char **)
1064         odr_malloc(odr, (10 + no_parms) * sizeof(*out_values));
1065
1066     // may be changed if it's a content connection
1067     std::string torus_url = m_p->torus_searchable_url;
1068     int i;
1069     for (i = 0; i < no_parms; i++)
1070     {
1071         const char *name = names[i];
1072         const char *value = values[i];
1073         assert(name);
1074         assert(value);
1075         if (!strcmp(name, "user"))
1076             param_user = value;
1077         else if (!strcmp(name, "password"))
1078             param_password = value;
1079         else if (!strcmp(name, "content-user"))
1080             param_content_user = value;
1081         else if (!strcmp(name, "content-password"))
1082             param_content_password = value;
1083         else if (!strcmp(name, "content-proxy"))
1084             content_proxy = value;
1085         else if (!strcmp(name, "nocproxy"))
1086             param_nocproxy = value;
1087         else if (!strcmp(name, "proxy"))
1088         {
1089             char **dstr;
1090             int dnum = 0;
1091             nmem_strsplit(((ODR) odr)->mem, ",", value, &dstr, &dnum);
1092             if (connection_reuse)
1093             {
1094                 // find the step after our current proxy
1095                 int i;
1096                 for (i = 0; i < dnum; i++)
1097                     if (!strcmp(proxy.c_str(), dstr[i]))
1098                         break;
1099                 if (i >= dnum - 1)
1100                     *proxy_step = 0;
1101                 else
1102                     *proxy_step = i + 1;
1103             }
1104             else
1105             {
1106                 // step is known.. Guess our proxy from it
1107                 if (*proxy_step >= dnum)
1108                     *proxy_step = 0;
1109                 else
1110                 {
1111                     proxy = dstr[*proxy_step];
1112
1113                     (*proxy_step)++;
1114                     if (*proxy_step == dnum)
1115                         *proxy_step = 0;
1116                 }
1117             }
1118         }
1119         else if (!strcmp(name, "cproxysession"))
1120         {
1121             out_names[no_out_args] = name;
1122             out_values[no_out_args++] = value;
1123             torus_url = m_p->torus_content_url;
1124         }
1125         else if (!strcmp(name, "realm") && session_realm.length() == 0)
1126             realm = value;
1127         else if (!strcmp(name, "torus_url") && session_realm.length() == 0)
1128             torus_url = value;
1129         else if (name[0] == 'x' && name[1] == '-')
1130         {
1131             out_names[no_out_args] = name;
1132             out_values[no_out_args++] = value;
1133         }
1134         else
1135         {
1136             BackendPtr notfound;
1137             char *msg = (char*) odr_malloc(odr, strlen(name) + 30);
1138             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1139             sprintf(msg, "zoom: bad database argument: %s", name);
1140             *addinfo = msg;
1141             return notfound;
1142         }
1143     }
1144     if (proxy.length())
1145         package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str());
1146
1147     if (connection_reuse)
1148     {
1149         m_backend->connect("", error, addinfo, odr);
1150         return m_backend;
1151     }
1152
1153     if (param_user)
1154     {
1155         authentication = std::string(param_user);
1156         if (param_password)
1157             authentication += "/" + std::string(param_password);
1158     }
1159     if (param_content_user)
1160     {
1161         content_authentication = std::string(param_content_user);
1162         if (param_content_password)
1163             content_authentication += "/" + std::string(param_content_password);
1164     }
1165
1166     if (torus_db.compare("IR-Explain---1") == 0)
1167         return explain_search(package, database, error, addinfo, odr, torus_url,
1168                               torus_db, realm);
1169
1170     SearchablePtr sptr;
1171
1172     std::map<std::string,SearchablePtr>::iterator it;
1173     it = m_p->s_map.find(torus_db);
1174     if (it != m_p->s_map.end())
1175         sptr = it->second;
1176     else if (torus_url.length() > 0)
1177     {
1178         std::string torus_query = "udb==" + torus_db;
1179         xmlDoc *doc = mp::get_searchable(package,torus_url, torus_db,
1180                                          torus_query,
1181                                          realm, m_p->proxy);
1182         if (!doc)
1183         {
1184             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1185             *addinfo = odr_strdup(odr, "Torus server unavailable or "
1186                                   "incorrectly configured");
1187             BackendPtr b;
1188             return b;
1189         }
1190         const xmlNode *ptr = xmlDocGetRootElement(doc);
1191         if (ptr && ptr->type == XML_ELEMENT_NODE)
1192         {
1193             if (!strcmp((const char *) ptr->name, "record"))
1194             {
1195                 sptr = m_p->parse_torus_record(ptr);
1196             }
1197             else if (!strcmp((const char *) ptr->name, "records"))
1198             {
1199                 for (ptr = ptr->children; ptr; ptr = ptr->next)
1200                 {
1201                     if (ptr->type == XML_ELEMENT_NODE
1202                         && !strcmp((const char *) ptr->name, "record"))
1203                     {
1204                         if (sptr)
1205                         {
1206                             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1207                             *addinfo = (char*)
1208                                 odr_malloc(odr, 40 + torus_db.length());
1209                             sprintf(*addinfo, "multiple records for udb=%s",
1210                                     database.c_str());
1211                             xmlFreeDoc(doc);
1212                             BackendPtr b;
1213                             return b;
1214                         }
1215                         sptr = m_p->parse_torus_record(ptr);
1216                     }
1217                 }
1218             }
1219             else
1220             {
1221                 *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1222                 *addinfo = (char*) odr_malloc(
1223                     odr, 40 + strlen((const char *) ptr->name));
1224                 sprintf(*addinfo, "bad root element for torus: %s", ptr->name);
1225                 xmlFreeDoc(doc);
1226                 BackendPtr b;
1227                 return b;
1228             }
1229         }
1230         xmlFreeDoc(doc);
1231     }
1232
1233     if (!sptr)
1234     {
1235         *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1236         *addinfo = odr_strdup(odr, torus_db.c_str());
1237         BackendPtr b;
1238         return b;
1239     }
1240
1241     xsltStylesheetPtr xsp = 0;
1242     if (sptr->transform_xsl_content.length())
1243     {
1244         xmlDoc *xsp_doc = xmlParseMemory(sptr->transform_xsl_content.c_str(),
1245                                          sptr->transform_xsl_content.length());
1246         if (!xsp_doc)
1247         {
1248             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1249             *addinfo = odr_strdup(odr, "zoom: xmlParseMemory failed "
1250                                   "for literalTransform XSL");
1251             BackendPtr b;
1252             return b;
1253         }
1254         xsp = xsltParseStylesheetDoc(xsp_doc);
1255         if (!xsp)
1256         {
1257             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1258             *addinfo =
1259                 odr_strdup(odr,"zoom: xsltParseStylesheetDoc failed "
1260                            "for literalTransform XSL");
1261             BackendPtr b;
1262             xmlFreeDoc(xsp_doc);
1263             return b;
1264         }
1265     }
1266     else if (sptr->transform_xsl_fname.length())
1267     {
1268         const char *path = 0;
1269
1270         if (m_p->xsldir.length())
1271             path = m_p->xsldir.c_str();
1272         else
1273             path = m_p->file_path.c_str();
1274         std::string fname;
1275
1276         char fullpath[1024];
1277         char *cp = yaz_filepath_resolve(sptr->transform_xsl_fname.c_str(),
1278                                         path, 0, fullpath);
1279         if (cp)
1280             fname.assign(cp);
1281         else
1282         {
1283             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1284             *addinfo = (char *)
1285                 odr_malloc(odr, 40 + sptr->transform_xsl_fname.length());
1286             sprintf(*addinfo, "zoom: could not open file %s",
1287                     sptr->transform_xsl_fname.c_str());
1288             BackendPtr b;
1289             return b;
1290         }
1291         xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
1292         if (!xsp_doc)
1293         {
1294             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1295             *addinfo = (char *) odr_malloc(odr, 50 + fname.length());
1296             sprintf(*addinfo, "zoom: xmlParseFile failed for file %s",
1297                     fname.c_str());
1298             BackendPtr b;
1299             return b;
1300         }
1301         xsp = xsltParseStylesheetDoc(xsp_doc);
1302         if (!xsp)
1303         {
1304             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1305             *addinfo = (char *) odr_malloc(odr, 50 + fname.length());
1306             sprintf(*addinfo, "zoom: xsltParseStylesheetDoc failed "
1307                     "for file %s", fname.c_str());
1308             BackendPtr b;
1309             xmlFreeDoc(xsp_doc);
1310             return b;
1311         }
1312     }
1313
1314     cql_transform_t cqlt = 0;
1315     if (sptr->rpn2cql_fname.length())
1316     {
1317         char fullpath[1024];
1318         char *cp = yaz_filepath_resolve(sptr->rpn2cql_fname.c_str(),
1319                                         m_p->file_path.c_str(), 0, fullpath);
1320         if (cp)
1321             cqlt = cql_transform_open_fname(fullpath);
1322     }
1323     else
1324         cqlt = cql_transform_create();
1325
1326     if (!cqlt)
1327     {
1328         *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1329         *addinfo = odr_strdup(odr, "zoom: missing/invalid cql2rpn file");
1330         BackendPtr b;
1331         xsltFreeStylesheet(xsp);
1332         return b;
1333     }
1334
1335     m_backend.reset();
1336
1337     BackendPtr b(new Backend);
1338
1339     b->cqlt = cqlt;
1340     b->sptr = sptr;
1341     b->xsp = xsp;
1342     b->m_frontend_database = database;
1343     b->enable_cproxy = param_nocproxy ? false : true;
1344
1345     if (sptr->query_encoding.length())
1346         b->set_option("rpnCharset", sptr->query_encoding);
1347
1348     if (sptr->extraArgs.length())
1349         b->set_option("extraArgs", sptr->extraArgs);
1350
1351     b->set_option("timeout", m_p->zoom_timeout.c_str());
1352
1353     if (m_p->apdu_log)
1354         b->set_option("apdulog", "1");
1355
1356     if (sptr->piggyback && sptr->sru.length())
1357         b->set_option("count", "1"); /* some SRU servers INSIST on getting
1358                                         maximumRecords > 0 */
1359     b->set_option("piggyback", sptr->piggyback ? "1" : "0");
1360
1361     if (authentication.length() == 0)
1362         authentication = sptr->authentication;
1363
1364     if (proxy.length() == 0)
1365         proxy = sptr->cfProxy;
1366     b->m_proxy = proxy;
1367
1368     if (sptr->cfAuth.length())
1369     {
1370         // A CF target
1371         b->set_option("user", sptr->cfAuth);
1372         if (authentication.length())
1373         {
1374             size_t found = authentication.find('/');
1375             if (found != std::string::npos)
1376             {
1377                 out_names[no_out_args] = "user";
1378                 out_values[no_out_args++] =
1379                     odr_strdup(odr, authentication.substr(0, found).c_str());
1380
1381                 out_names[no_out_args] = "password";
1382                 out_values[no_out_args++] =
1383                     odr_strdup(odr, authentication.substr(found+1).c_str());
1384             }
1385             else
1386             {
1387                 out_names[no_out_args] = "user";
1388                 out_values[no_out_args++] =
1389                     odr_strdup(odr, authentication.c_str());
1390             }
1391         }
1392         if (proxy.length())
1393         {
1394             out_names[no_out_args] = "proxy";
1395             out_values[no_out_args++] = odr_strdup(odr, proxy.c_str());
1396         }
1397         if (sptr->cfSubDB.length())
1398         {
1399             out_names[no_out_args] = "subdatabase";
1400             out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str());
1401         }
1402         if (param_nocproxy)
1403         {
1404             out_names[no_out_args] = "nocproxy";
1405             out_values[no_out_args++] = odr_strdup(odr, param_nocproxy);
1406         }
1407     }
1408     else
1409     {
1410         if (sptr->sru.length() == 0)
1411             b->set_option("user", authentication); /* Z39.50 */
1412         else
1413         {
1414             std::string user;
1415             std::string password;
1416             std::string authtype;
1417             {
1418                 const char *cstr = authentication.c_str();
1419                 const char *cp1 = strchr(cstr, '/');
1420                 const char *cp2 = 0;
1421                 if (cp1)
1422                 {
1423                     cp2 = strchr(cp1 + 1, '/');
1424                     if (cp2)
1425                     {
1426                         password.assign(cp1 + 1, cp2 - cp1 - 1);
1427                         authtype.assign(cp2 + 1);
1428                     }
1429                     else
1430                         password.assign(cp1 + 1);
1431                     user.assign(cstr, cp1 - cstr);
1432                 }
1433                 else
1434                     user.assign(cstr);
1435             }
1436
1437             if (authtype.compare("url") == 0)
1438             {
1439                 /* SRU URL encoding of auth stuff */
1440                 ODR o = odr_createmem(ODR_ENCODE);
1441                 char *path = 0;
1442                 const char *names[3];
1443                 const char *values[3];
1444
1445                 names[0] = "x-username";
1446                 values[0] = user.c_str();
1447                 names[1] = "x-password";
1448                 values[1] = password.c_str();
1449                 names[2] = 0;
1450                 values[2] = 0;
1451
1452                 yaz_array_to_uri(&path, o, (char **) names, (char **) values);
1453                 b->set_option("extraArgs", path);
1454                 odr_destroy(o);
1455             }
1456             else
1457             {
1458                 b->set_option("user", user);
1459                 if (password.length())
1460                     b->set_option("password", password);
1461             }
1462         }
1463         if (proxy.length())
1464             b->set_option("proxy", proxy);
1465     }
1466     std::string url(sptr->target);
1467     if (sptr->sru.length())
1468     {
1469         b->set_option("sru", sptr->sru);
1470         if (url.find("://") == std::string::npos)
1471             url = "http://" + url;
1472         if (sptr->sru_version.length())
1473             b->set_option("sru_version", sptr->sru_version);
1474     }
1475     if (no_out_args)
1476     {
1477         char *x_args = 0;
1478         out_names[no_out_args] = 0; // terminate list
1479
1480         yaz_array_to_uri(&x_args, odr, (char **) out_names,
1481                          (char **) out_values);
1482         url += "," + std::string(x_args);
1483     }
1484     package.log("zoom", YLOG_LOG, "url: %s", url.c_str());
1485     b->connect(url, error, addinfo, odr);
1486     if (*error == 0 && b->enable_cproxy)
1487         create_content_session(package, b, error, addinfo, odr,
1488                                content_authentication.length() ?
1489                                content_authentication : authentication,
1490                                content_proxy.length() ? content_proxy : proxy,
1491                                realm);
1492     if (*error == 0)
1493         m_backend = b;
1494     return b;
1495 }
1496
1497 void yf::Zoom::Frontend::prepare_elements(BackendPtr b,
1498                                           Odr_oid *preferredRecordSyntax,
1499                                           const char *element_set_name,
1500                                           bool &enable_pz2_retrieval,
1501                                           bool &enable_pz2_transform,
1502                                           bool &enable_record_transform,
1503                                           bool &assume_marc8_charset)
1504 {
1505     char oid_name_str[OID_STR_MAX];
1506     const char *syntax_name = 0;
1507
1508     if (preferredRecordSyntax &&
1509         !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
1510     {
1511         if (element_set_name &&
1512             !strcmp(element_set_name, m_p->element_transform.c_str()))
1513         {
1514             enable_pz2_retrieval = true;
1515             enable_pz2_transform = true;
1516         }
1517         else if (element_set_name &&
1518                  !strcmp(element_set_name, m_p->element_raw.c_str()))
1519         {
1520             enable_pz2_retrieval = true;
1521         }
1522         else if (m_p->record_xsp)
1523         {
1524             enable_pz2_retrieval = true;
1525             enable_pz2_transform = true;
1526             enable_record_transform = true;
1527         }
1528     }
1529
1530     if (enable_pz2_retrieval)
1531     {
1532         std::string configured_request_syntax = b->sptr->request_syntax;
1533         if (configured_request_syntax.length())
1534         {
1535             syntax_name = configured_request_syntax.c_str();
1536             const Odr_oid *syntax_oid =
1537                 yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
1538             if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
1539                 || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
1540                 assume_marc8_charset = true;
1541         }
1542     }
1543     else if (preferredRecordSyntax)
1544         syntax_name =
1545             yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
1546
1547     if (b->sptr->sru.length())
1548         syntax_name = "XML";
1549
1550     b->set_option("preferredRecordSyntax", syntax_name);
1551
1552     if (enable_pz2_retrieval)
1553     {
1554         if (element_set_name && !strcmp(element_set_name,
1555                                         m_p->element_passthru.c_str()))
1556             ;
1557         else
1558         {
1559             element_set_name = 0;
1560             if (b->sptr->element_set.length())
1561                 element_set_name = b->sptr->element_set.c_str();
1562         }
1563     }
1564
1565     b->set_option("elementSetName", element_set_name);
1566     if (b->sptr->sru.length() && element_set_name)
1567         b->set_option("schema", element_set_name);
1568 }
1569
1570 Z_Records *yf::Zoom::Frontend::get_explain_records(
1571     mp::Package &package,
1572     Odr_int start,
1573     Odr_int number_to_present,
1574     int *error,
1575     char **addinfo,
1576     Odr_int *number_of_records_returned,
1577     ODR odr,
1578     BackendPtr b,
1579     Odr_oid *preferredRecordSyntax,
1580     const char *element_set_name)
1581 {
1582     Odr_int i;
1583     Z_Records *records = 0;
1584
1585     if (!b->explain_doc)
1586     {
1587         return records;
1588     }
1589     if (number_to_present > 10000)
1590         number_to_present = 10000;
1591
1592     xmlNode *ptr = xmlDocGetRootElement(b->explain_doc);
1593
1594     Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1595         odr_malloc(odr, sizeof(*npl));
1596     npl->records = (Z_NamePlusRecord **)
1597         odr_malloc(odr, number_to_present * sizeof(*npl->records));
1598
1599     for (i = 0; i < number_to_present; i++)
1600     {
1601         int num = 0;
1602         xmlNode *res = xml_node_search(ptr, &num, start + i + 1);
1603         if (!res)
1604             break;
1605         xmlBufferPtr xml_buf = xmlBufferCreate();
1606         xmlNode *tmp_node = xmlCopyNode(res->children, 1);
1607         xmlNodeDump(xml_buf, tmp_node->doc, tmp_node, 0, 0);
1608
1609         Z_NamePlusRecord *npr =
1610             (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1611         npr->databaseName = odr_strdup(odr, b->m_frontend_database.c_str());
1612         npr->which = Z_NamePlusRecord_databaseRecord;
1613         npr->u.databaseRecord =
1614             z_ext_record_xml(odr,
1615                              (const char *) xml_buf->content, xml_buf->use);
1616         npl->records[i] = npr;
1617         xmlFreeNode(tmp_node);
1618         xmlBufferFree(xml_buf);
1619     }
1620     records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1621     records->which = Z_Records_DBOSD;
1622     records->u.databaseOrSurDiagnostics = npl;
1623
1624     npl->num_records = i;
1625     *number_of_records_returned = i;
1626     return records;
1627 }
1628
1629
1630 Z_Records *yf::Zoom::Frontend::get_records(mp::Package &package,
1631                                            Odr_int start,
1632                                            Odr_int number_to_present,
1633                                            int *error,
1634                                            char **addinfo,
1635                                            Odr_int *number_of_records_returned,
1636                                            ODR odr,
1637                                            BackendPtr b,
1638                                            Odr_oid *preferredRecordSyntax,
1639                                            const char *element_set_name)
1640 {
1641     *number_of_records_returned = 0;
1642     Z_Records *records = 0;
1643     bool enable_pz2_retrieval = false; // whether target profile is used
1644     bool enable_pz2_transform = false; // whether XSLT is used as well
1645     bool assume_marc8_charset = false;
1646     bool enable_record_transform = false;
1647
1648     prepare_elements(b, preferredRecordSyntax,
1649                      element_set_name,
1650                      enable_pz2_retrieval,
1651                      enable_pz2_transform,
1652                      enable_record_transform,
1653                      assume_marc8_charset);
1654
1655     package.log("zoom", YLOG_LOG, "pz2_retrieval: %s . pz2_transform: %s",
1656                 enable_pz2_retrieval ? "yes" : "no",
1657                 enable_pz2_transform ? "yes" : "no");
1658
1659     if (start < 0 || number_to_present <=0)
1660         return records;
1661
1662     if (number_to_present > 10000)
1663         number_to_present = 10000;
1664
1665     ZOOM_record *recs = (ZOOM_record *)
1666         odr_malloc(odr, (size_t) number_to_present * sizeof(*recs));
1667
1668     b->present(start, number_to_present, recs, error, addinfo, odr);
1669
1670     int i = 0;
1671     if (!*error)
1672     {
1673         for (i = 0; i < number_to_present; i++)
1674             if (!recs[i])
1675                 break;
1676     }
1677     if (i > 0)
1678     {  // only return records if no error and at least one record
1679
1680         const char *xsl_parms[3];
1681         mp::wrbuf cproxy_host;
1682
1683         if (b->enable_cproxy && b->cproxy_host.length())
1684         {
1685             wrbuf_puts(cproxy_host, "\"");
1686             wrbuf_puts(cproxy_host, b->cproxy_host.c_str());
1687             wrbuf_puts(cproxy_host, "/\"");
1688
1689             xsl_parms[0] = "cproxyhost";
1690             xsl_parms[1] = wrbuf_cstr(cproxy_host);
1691             xsl_parms[2] = 0;
1692         }
1693         else
1694         {
1695             xsl_parms[0] = 0;
1696         }
1697
1698         char *odr_database = odr_strdup(odr,
1699                                         b->m_frontend_database.c_str());
1700         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1701             odr_malloc(odr, sizeof(*npl));
1702         *number_of_records_returned = i;
1703         npl->num_records = i;
1704         npl->records = (Z_NamePlusRecord **)
1705             odr_malloc(odr, i * sizeof(*npl->records));
1706         for (i = 0; i < number_to_present; i++)
1707         {
1708             Z_NamePlusRecord *npr = 0;
1709             const char *addinfo;
1710
1711             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
1712                                               &addinfo, 0 /* diagset */);
1713
1714             if (sur_error)
1715             {
1716                 log_diagnostic(package, sur_error, addinfo);
1717                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
1718                                             addinfo);
1719             }
1720             else if (enable_pz2_retrieval)
1721             {
1722                 char rec_type_str[100];
1723                 const char *record_encoding = 0;
1724
1725                 if (b->sptr->record_encoding.length())
1726                     record_encoding = b->sptr->record_encoding.c_str();
1727                 else if (assume_marc8_charset)
1728                     record_encoding = "marc8";
1729
1730                 strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
1731                 if (record_encoding)
1732                 {
1733                     strcat(rec_type_str, "; charset=");
1734                     strcat(rec_type_str, record_encoding);
1735                 }
1736
1737                 package.log("zoom", YLOG_LOG, "Getting record of type %s",
1738                             rec_type_str);
1739                 int rec_len;
1740                 xmlChar *xmlrec_buf = 0;
1741                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
1742                                                       &rec_len);
1743                 if (!rec_buf && !npr)
1744                 {
1745                     std::string addinfo("ZOOM_record_get failed for type ");
1746
1747                     int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1748                     addinfo += rec_type_str;
1749                     log_diagnostic(package, error, addinfo.c_str());
1750                     npr = zget_surrogateDiagRec(odr, odr_database,
1751                                                 error, addinfo.c_str());
1752                 }
1753                 else
1754                 {
1755                     package.log_write(rec_buf, rec_len);
1756                     package.log_write("\r\n", 2);
1757                 }
1758
1759                 if (rec_buf && b->xsp && enable_pz2_transform)
1760                 {
1761                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
1762                     if (!rec_doc)
1763                     {
1764                         const char *addinfo = "xml parse failed for record";
1765                         int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1766                         log_diagnostic(package, error, addinfo);
1767                         npr = zget_surrogateDiagRec(
1768                             odr, odr_database, error, addinfo);
1769                     }
1770                     else
1771                     {
1772                         // first stage XSLT - per target
1773                         xsltStylesheetPtr xsp = b->xsp;
1774                         xmlDoc *rec_res = xsltApplyStylesheet(xsp, rec_doc,
1775                                                               xsl_parms);
1776                         // insert generated-url
1777                         if (rec_res)
1778                         {
1779                             std::string res =
1780                                 mp::xml::url_recipe_handle(rec_res,
1781                                                            b->sptr->urlRecipe);
1782                             if (res.length())
1783                             {
1784                                 xmlNode *ptr = xmlDocGetRootElement(rec_res);
1785                                 while (ptr && ptr->type != XML_ELEMENT_NODE)
1786                                     ptr = ptr->next;
1787                                 xmlNode *c =
1788                                     xmlNewChild(ptr, 0, BAD_CAST "metadata", 0);
1789                                 xmlNewProp(c, BAD_CAST "type", BAD_CAST
1790                                            "generated-url");
1791                                 xmlNode * t = xmlNewText(BAD_CAST res.c_str());
1792                                 xmlAddChild(c, t);
1793                             }
1794                         }
1795                         // second stage XSLT - common
1796                         if (rec_res && m_p->record_xsp &&
1797                             enable_record_transform)
1798                         {
1799                             xmlDoc *tmp_doc = rec_res;
1800
1801                             xsp = m_p->record_xsp;
1802                             rec_res = xsltApplyStylesheet(xsp, tmp_doc,
1803                                                           xsl_parms);
1804                             xmlFreeDoc(tmp_doc);
1805                         }
1806                         // get result out of it
1807                         if (rec_res)
1808                         {
1809                             xsltSaveResultToString(&xmlrec_buf, &rec_len,
1810                                                    rec_res, xsp);
1811                             rec_buf = (const char *) xmlrec_buf;
1812                             package.log_write(rec_buf, rec_len);
1813
1814                             xmlFreeDoc(rec_res);
1815                         }
1816                         if (!rec_buf)
1817                         {
1818                             std::string addinfo;
1819                             int error =
1820                                 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1821
1822                             addinfo = "xslt apply failed for "
1823                                 + b->sptr->transform_xsl_fname;
1824                             log_diagnostic(package, error, addinfo.c_str());
1825                             npr = zget_surrogateDiagRec(
1826                                 odr, odr_database, error, addinfo.c_str());
1827                         }
1828                         xmlFreeDoc(rec_doc);
1829                     }
1830                 }
1831
1832                 if (!npr)
1833                 {
1834                     if (!rec_buf)
1835                         npr = zget_surrogateDiagRec(
1836                             odr, odr_database,
1837                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1838                             rec_type_str);
1839                     else
1840                     {
1841                         npr = (Z_NamePlusRecord *)
1842                             odr_malloc(odr, sizeof(*npr));
1843                         npr->databaseName = odr_database;
1844                         npr->which = Z_NamePlusRecord_databaseRecord;
1845                         npr->u.databaseRecord =
1846                             z_ext_record_xml(odr, rec_buf, rec_len);
1847                     }
1848                 }
1849                 if (xmlrec_buf)
1850                     xmlFree(xmlrec_buf);
1851             }
1852             else
1853             {
1854                 Z_External *ext =
1855                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
1856                 if (ext)
1857                 {
1858                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1859                     npr->databaseName = odr_database;
1860                     npr->which = Z_NamePlusRecord_databaseRecord;
1861                     npr->u.databaseRecord = ext;
1862                 }
1863                 else
1864                 {
1865                     npr = zget_surrogateDiagRec(
1866                         odr, odr_database,
1867                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1868                         "ZOOM_record, type ext");
1869                 }
1870             }
1871             npl->records[i] = npr;
1872         }
1873         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1874         records->which = Z_Records_DBOSD;
1875         records->u.databaseOrSurDiagnostics = npl;
1876     }
1877     return records;
1878 }
1879
1880 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
1881                                                     ODR odr)
1882 {
1883     struct cql_node *r = 0;
1884     if (!cn)
1885         return 0;
1886     switch (cn->which)
1887     {
1888     case CQL_NODE_ST:
1889         if (cn->u.st.index)
1890         {
1891             std::map<std::string,std::string>::const_iterator it;
1892             it = fieldmap.find(cn->u.st.index);
1893             if (it == fieldmap.end())
1894                 return cn;
1895             if (it->second.length())
1896                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
1897             else
1898                 cn->u.st.index = 0;
1899         }
1900         break;
1901     case CQL_NODE_BOOL:
1902         r = convert_cql_fields(cn->u.boolean.left, odr);
1903         if (!r)
1904             r = convert_cql_fields(cn->u.boolean.right, odr);
1905         break;
1906     case CQL_NODE_SORT:
1907         r = convert_cql_fields(cn->u.sort.search, odr);
1908         break;
1909     }
1910     return r;
1911 }
1912
1913 void yf::Zoom::Frontend::log_diagnostic(mp::Package &package,
1914                                         int error, const char *addinfo)
1915 {
1916     const char *err_msg = yaz_diag_bib1_str(error);
1917     if (addinfo)
1918         package.log("zoom", YLOG_WARN, "Diagnostic %d %s: %s",
1919                     error, err_msg, addinfo);
1920     else
1921         package.log("zoom", YLOG_WARN, "Diagnostic %d %s:",
1922                     error, err_msg);
1923 }
1924
1925 yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package,
1926                                                         std::string &database,
1927                                                         int *error,
1928                                                         char **addinfo,
1929                                                         mp::odr &odr,
1930                                                         std::string torus_url,
1931                                                         std::string &torus_db,
1932                                                         std::string &realm)
1933 {
1934     m_backend.reset();
1935
1936     BackendPtr b(new Backend);
1937
1938     b->m_frontend_database = database;
1939     b->enable_explain = true;
1940
1941     Z_GDU *gdu = package.request().get();
1942     Z_APDU *apdu_req = gdu->u.z3950;
1943     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1944     Z_Query *query = sr->query;
1945
1946     if (!m_p->explain_xsp)
1947     {
1948         *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1949         *addinfo =
1950             odr_strdup(odr, "IR-Explain---1 unsupported. "
1951                        "Torus explain_xsl not defined");
1952         return m_backend;
1953     }
1954     else if (query->which == Z_Query_type_104 &&
1955         query->u.type_104->which == Z_External_CQL)
1956     {
1957         std::string torus_query(query->u.type_104->u.cql);
1958         xmlDoc *doc = mp::get_searchable(package, torus_url, "",
1959                                          torus_query,
1960                                          realm, m_p->proxy);
1961         if (m_p->explain_xsp)
1962         {
1963             xmlDoc *rec_res =  xsltApplyStylesheet(m_p->explain_xsp, doc, 0);
1964
1965             xmlFreeDoc(doc);
1966             doc = rec_res;
1967         }
1968         if (!doc)
1969         {
1970             *error = YAZ_BIB1_UNSPECIFIED_ERROR;
1971             *addinfo = odr_strdup(odr, "Torus server unavailable or "
1972                                   "incorrectly configured");
1973         }
1974         else
1975         {
1976             xmlNode *ptr = xmlDocGetRootElement(doc);
1977             int hits = 0;
1978
1979             xml_node_search(ptr, &hits, 0);
1980
1981             Z_APDU *apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
1982             apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1983             package.response() = apdu_res;
1984             m_backend = b;
1985         }
1986         if (b->explain_doc)
1987             xmlFreeDoc(b->explain_doc);
1988         b->explain_doc = doc;
1989         return m_backend;
1990     }
1991     else
1992     {
1993         *error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
1994         *addinfo = odr_strdup(odr, "IR-Explain---1 only supports CQL");
1995         return m_backend;
1996     }
1997 }
1998
1999 static bool wait_conn(COMSTACK cs, int secs)
2000 {
2001     struct yaz_poll_fd pfd;
2002
2003     yaz_poll_add(pfd.input_mask, yaz_poll_except);
2004     if (cs->io_pending && CS_WANT_WRITE)
2005         yaz_poll_add(pfd.input_mask, yaz_poll_write);
2006     if (cs->io_pending & CS_WANT_READ)
2007         yaz_poll_add(pfd.input_mask, yaz_poll_read);
2008
2009     pfd.fd = cs_fileno(cs);
2010     pfd.client_data = 0;
2011
2012     int ret = yaz_poll(&pfd, 1, secs, 0);
2013     return ret > 0;
2014 }
2015
2016 bool yf::Zoom::Impl::check_proxy(const char *proxy)
2017 {
2018     COMSTACK conn = 0;
2019     const char *uri = "http://localhost/";
2020     void *add;
2021     mp::odr odr;
2022     bool outcome = false;
2023     conn = cs_create_host_proxy(uri, 0, &add, proxy);
2024
2025     if (!conn)
2026         return false;
2027
2028     Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1);
2029     gdu->u.HTTP_Request->method = odr_strdup(odr, "GET");
2030
2031     if (z_GDU(odr, &gdu, 0, 0))
2032     {
2033         int len;
2034         char *buf = odr_getbuf(odr, &len, 0);
2035
2036         int ret = cs_connect(conn, add);
2037         if (ret > 0 || (ret == 0 && wait_conn(conn, 1)))
2038         {
2039             while (1)
2040             {
2041                 ret = cs_put(conn, buf, len);
2042                 if (ret != 1)
2043                     break;
2044                 if (!wait_conn(conn, proxy_timeout))
2045                     break;
2046             }
2047             if (ret == 0)
2048                 outcome = true;
2049         }
2050     }
2051     cs_close(conn);
2052     return outcome;
2053 }
2054
2055 bool yf::Zoom::Frontend::retry(mp::Package &package,
2056                                mp::odr &odr,
2057                                BackendPtr b,
2058                                int &error, char **addinfo,
2059                                int &proxy_step, int &same_retries,
2060                                int &proxy_retries)
2061 {
2062     if (b && b->m_proxy.length() && !m_p->check_proxy(b->m_proxy.c_str()))
2063     {
2064         log_diagnostic(package, error, *addinfo);
2065         package.log("zoom", YLOG_LOG, "proxy %s fails", b->m_proxy.c_str());
2066         m_backend.reset();
2067         if (proxy_step) // there is a failover
2068         {
2069             proxy_retries++;
2070             package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
2071             return true;
2072         }
2073         error = YAZ_BIB1_PROXY_FAILURE;
2074         *addinfo = odr_strdup(odr, b->m_proxy.c_str());
2075     }
2076     else if (same_retries == 0 && proxy_retries == 0)
2077     {
2078         log_diagnostic(package, error, *addinfo);
2079         same_retries++;
2080         package.log("zoom", YLOG_WARN, "search failed: retry");
2081         m_backend.reset();
2082         proxy_step = 0;
2083         return true;
2084     }
2085     return false;
2086 }
2087
2088 void yf::Zoom::Frontend::handle_search(mp::Package &package)
2089 {
2090     Z_GDU *gdu = package.request().get();
2091     Z_APDU *apdu_req = gdu->u.z3950;
2092     Z_APDU *apdu_res = 0;
2093     mp::odr odr;
2094     Z_SearchRequest *sr = apdu_req->u.searchRequest;
2095     if (sr->num_databaseNames != 1)
2096     {
2097         int error = YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED;
2098         log_diagnostic(package, error, 0);
2099         apdu_res = odr.create_searchResponse(apdu_req, error, 0);
2100         package.response() = apdu_res;
2101         return;
2102     }
2103     int proxy_step = 0;
2104     int same_retries = 0;
2105     int proxy_retries = 0;
2106
2107 next_proxy:
2108
2109     int error = 0;
2110     char *addinfo = 0;
2111     std::string db(sr->databaseNames[0]);
2112
2113     BackendPtr b = get_backend_from_databases(package, db, &error,
2114                                               &addinfo, odr, &proxy_step);
2115     if (error)
2116     {
2117         if (retry(package, odr, b, error, &addinfo, proxy_step,
2118                   same_retries, proxy_retries))
2119             goto next_proxy;
2120     }
2121     if (error)
2122     {
2123         log_diagnostic(package, error, addinfo);
2124         apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2125         package.response() = apdu_res;
2126         return;
2127     }
2128     if (!b || b->enable_explain)
2129         return;
2130
2131     b->set_option("setname", "default");
2132
2133     bool enable_pz2_retrieval = false;
2134     bool enable_pz2_transform = false;
2135     bool enable_record_transform = false;
2136     bool assume_marc8_charset = false;
2137     prepare_elements(b, sr->preferredRecordSyntax, 0 /*element_set_name */,
2138                      enable_pz2_retrieval,
2139                      enable_pz2_transform,
2140                      enable_record_transform,
2141                      assume_marc8_charset);
2142
2143     Odr_int hits = 0;
2144     Z_Query *query = sr->query;
2145     mp::wrbuf ccl_wrbuf;
2146     mp::wrbuf pqf_wrbuf;
2147     std::string sortkeys;
2148
2149     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
2150     {
2151         // RPN
2152         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
2153     }
2154     else if (query->which == Z_Query_type_2)
2155     {
2156         // CCL
2157         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
2158                     query->u.type_2->len);
2159     }
2160     else if (query->which == Z_Query_type_104 &&
2161              query->u.type_104->which == Z_External_CQL)
2162     {
2163         // CQL
2164         const char *cql = query->u.type_104->u.cql;
2165         CQL_parser cp = cql_parser_create();
2166         int r = cql_parser_string(cp, cql);
2167         package.log("zoom", YLOG_LOG, "CQL: %s", cql);
2168         if (r)
2169         {
2170             cql_parser_destroy(cp);
2171             error = YAZ_BIB1_MALFORMED_QUERY;
2172             const char *addinfo = "CQL syntax error";
2173             log_diagnostic(package, error, addinfo);
2174             apdu_res =
2175                 odr.create_searchResponse(apdu_req, error, addinfo);
2176             package.response() = apdu_res;
2177             return;
2178         }
2179         struct cql_node *cn = cql_parser_result(cp);
2180         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
2181         if (cn_error)
2182         {
2183             // hopefully we are getting a ptr to a index+relation+term node
2184             error = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
2185             addinfo = 0;
2186             if (cn_error->which == CQL_NODE_ST)
2187                 addinfo = cn_error->u.st.index;
2188
2189             log_diagnostic(package, error, addinfo);
2190             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2191             package.response() = apdu_res;
2192             cql_parser_destroy(cp);
2193             return;
2194         }
2195         r = cql_to_ccl(cn, wrbuf_vp_puts,  ccl_wrbuf);
2196         if (r)
2197         {
2198             error = YAZ_BIB1_MALFORMED_QUERY;
2199             const char *addinfo = "CQL to CCL conversion error";
2200
2201             log_diagnostic(package, error, addinfo);
2202             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2203             package.response() = apdu_res;
2204             cql_parser_destroy(cp);
2205             return;
2206         }
2207
2208         mp::wrbuf sru_sortkeys_wrbuf;
2209         if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf))
2210         {
2211             error = YAZ_BIB1_ILLEGAL_SORT_RELATION;
2212             const char *addinfo = "CQL to CCL sortby conversion";
2213
2214             log_diagnostic(package, error, addinfo);
2215             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2216             package.response() = apdu_res;
2217             cql_parser_destroy(cp);
2218             return;
2219         }
2220         mp::wrbuf sort_spec_wrbuf;
2221         yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
2222                                       sort_spec_wrbuf);
2223         yaz_tok_cfg_t tc = yaz_tok_cfg_create();
2224         yaz_tok_parse_t tp =
2225             yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf));
2226         yaz_tok_cfg_destroy(tc);
2227
2228         /* go through sortspec and map fields */
2229         int token = yaz_tok_move(tp);
2230         while (token != YAZ_TOK_EOF)
2231         {
2232             if (token == YAZ_TOK_STRING)
2233             {
2234                 const char *field = yaz_tok_parse_string(tp);
2235                 std::map<std::string,std::string>::iterator it;
2236                 it = b->sptr->sortmap.find(field);
2237                 if (it != b->sptr->sortmap.end())
2238                     sortkeys += it->second;
2239                 else
2240                     sortkeys += field;
2241             }
2242             sortkeys += " ";
2243             token = yaz_tok_move(tp);
2244             if (token == YAZ_TOK_STRING)
2245             {
2246                 sortkeys += yaz_tok_parse_string(tp);
2247             }
2248             if (token != YAZ_TOK_EOF)
2249             {
2250                 sortkeys += " ";
2251                 token = yaz_tok_move(tp);
2252             }
2253         }
2254         yaz_tok_parse_destroy(tp);
2255         cql_parser_destroy(cp);
2256     }
2257     else
2258     {
2259         error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
2260         const char *addinfo = 0;
2261         log_diagnostic(package, error, addinfo);
2262         apdu_res =  odr.create_searchResponse(apdu_req, error, addinfo);
2263         package.response() = apdu_res;
2264         return;
2265     }
2266
2267     if (ccl_wrbuf.len())
2268     {
2269         // CCL to PQF
2270         assert(pqf_wrbuf.len() == 0);
2271         int cerror, cpos;
2272         struct ccl_rpn_node *cn;
2273         package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
2274         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
2275                           &cerror, &cpos);
2276         if (!cn)
2277         {
2278             char *addinfo = odr_strdup_null(odr, ccl_err_msg(cerror));
2279             error = YAZ_BIB1_MALFORMED_QUERY;
2280
2281             switch (cerror)
2282             {
2283             case CCL_ERR_UNKNOWN_QUAL:
2284             case CCL_ERR_TRUNC_NOT_LEFT:
2285             case CCL_ERR_TRUNC_NOT_RIGHT:
2286             case CCL_ERR_TRUNC_NOT_BOTH:
2287 #ifdef CCL_ERR_TRUNC_NOT_EMBED
2288             case CCL_ERR_TRUNC_NOT_EMBED:
2289 #endif
2290 #ifdef CCL_ERR_TRUNC_NOT_SINGLE
2291             case CCL_ERR_TRUNC_NOT_SINGLE:
2292 #endif
2293                 error = YAZ_BIB1_UNSUPP_SEARCH;
2294                 break;
2295             }
2296             log_diagnostic(package, error, addinfo);
2297             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2298             package.response() = apdu_res;
2299             return;
2300         }
2301         ccl_pquery(pqf_wrbuf, cn);
2302         package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf));
2303         ccl_rpn_delete(cn);
2304     }
2305
2306     assert(pqf_wrbuf.len());
2307
2308     ZOOM_query q = ZOOM_query_create();
2309     ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str());
2310
2311     if (b->get_option("sru"))
2312     {
2313         int status = 0;
2314         Z_RPNQuery *zquery;
2315         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
2316         mp::wrbuf wrb;
2317
2318         if (!strcmp(b->get_option("sru"), "solr"))
2319         {
2320             solr_transform_t cqlt = solr_transform_create();
2321
2322             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
2323
2324             solr_transform_close(cqlt);
2325         }
2326         else
2327         {
2328             status = cql_transform_rpn2cql_wrbuf(b->cqlt, wrb, zquery);
2329         }
2330         if (status == 0)
2331         {
2332             ZOOM_query_cql(q, wrbuf_cstr(wrb));
2333             package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb));
2334             b->search(q, &hits, &error, &addinfo, odr);
2335         }
2336         ZOOM_query_destroy(q);
2337
2338         if (status)
2339         {
2340             error = YAZ_BIB1_MALFORMED_QUERY;
2341             const char *addinfo = "can not convert from RPN to CQL/Solr";
2342             log_diagnostic(package, error, addinfo);
2343             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2344             package.response() = apdu_res;
2345             return;
2346         }
2347     }
2348     else
2349     {
2350         ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf));
2351         package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
2352         b->search(q, &hits, &error, &addinfo, odr);
2353         ZOOM_query_destroy(q);
2354     }
2355
2356     if (error)
2357     {
2358         if (retry(package, odr, b, error, &addinfo, proxy_step,
2359                   same_retries, proxy_retries))
2360             goto next_proxy;
2361     }
2362
2363     const char *element_set_name = 0;
2364     Odr_int number_to_present = 0;
2365     if (!error)
2366         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
2367
2368     Odr_int number_of_records_returned = 0;
2369     Z_Records *records = get_records(
2370         package,
2371         0, number_to_present, &error, &addinfo,
2372         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
2373         element_set_name);
2374     if (error)
2375         log_diagnostic(package, error, addinfo);
2376     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
2377     if (records)
2378     {
2379         apdu_res->u.searchResponse->records = records;
2380         apdu_res->u.searchResponse->numberOfRecordsReturned =
2381             odr_intdup(odr, number_of_records_returned);
2382     }
2383     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
2384     package.response() = apdu_res;
2385 }
2386
2387 void yf::Zoom::Frontend::handle_present(mp::Package &package)
2388 {
2389     Z_GDU *gdu = package.request().get();
2390     Z_APDU *apdu_req = gdu->u.z3950;
2391     Z_APDU *apdu_res = 0;
2392     Z_PresentRequest *pr = apdu_req->u.presentRequest;
2393
2394     mp::odr odr;
2395     if (!m_backend)
2396     {
2397         package.response() = odr.create_presentResponse(
2398             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
2399         return;
2400     }
2401     const char *element_set_name = 0;
2402     Z_RecordComposition *comp = pr->recordComposition;
2403     if (comp && comp->which != Z_RecordComp_simple)
2404     {
2405         package.response() = odr.create_presentResponse(
2406             apdu_req,
2407             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
2408         return;
2409     }
2410     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
2411         element_set_name = comp->u.simple->u.generic;
2412     Odr_int number_of_records_returned = 0;
2413     int error = 0;
2414     char *addinfo = 0;
2415
2416     if (m_backend->enable_explain)
2417     {
2418         Z_Records *records =
2419             get_explain_records(
2420                 package,
2421                 *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
2422                 &error, &addinfo, &number_of_records_returned, odr, m_backend,
2423                 pr->preferredRecordSyntax, element_set_name);
2424
2425         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
2426         if (records)
2427         {
2428             apdu_res->u.presentResponse->records = records;
2429             apdu_res->u.presentResponse->numberOfRecordsReturned =
2430                 odr_intdup(odr, number_of_records_returned);
2431         }
2432         package.response() = apdu_res;
2433     }
2434     else
2435     {
2436         Z_Records *records =
2437             get_records(package,
2438                         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
2439                         &error, &addinfo, &number_of_records_returned, odr, m_backend,
2440                         pr->preferredRecordSyntax, element_set_name);
2441
2442         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
2443         if (records)
2444         {
2445             apdu_res->u.presentResponse->records = records;
2446             apdu_res->u.presentResponse->numberOfRecordsReturned =
2447                 odr_intdup(odr, number_of_records_returned);
2448         }
2449         package.response() = apdu_res;
2450     }
2451 }
2452
2453 void yf::Zoom::Frontend::handle_package(mp::Package &package)
2454 {
2455     Z_GDU *gdu = package.request().get();
2456     if (!gdu)
2457         ;
2458     else if (gdu->which == Z_GDU_Z3950)
2459     {
2460         Z_APDU *apdu_req = gdu->u.z3950;
2461
2462         if (m_backend)
2463             wrbuf_rewind(m_backend->m_apdu_wrbuf);
2464         if (apdu_req->which == Z_APDU_initRequest)
2465         {
2466             mp::odr odr;
2467             package.response() = odr.create_close(
2468                 apdu_req,
2469                 Z_Close_protocolError,
2470                 "double init");
2471         }
2472         else if (apdu_req->which == Z_APDU_searchRequest)
2473         {
2474             handle_search(package);
2475         }
2476         else if (apdu_req->which == Z_APDU_presentRequest)
2477         {
2478             handle_present(package);
2479         }
2480         else
2481         {
2482             mp::odr odr;
2483             package.response() = odr.create_close(
2484                 apdu_req,
2485                 Z_Close_protocolError,
2486                 "zoom filter cannot handle this APDU");
2487             package.session().close();
2488         }
2489         if (m_backend)
2490         {
2491             WRBUF w = m_backend->m_apdu_wrbuf;
2492             package.log_write(wrbuf_buf(w), wrbuf_len(w));
2493         }
2494     }
2495     else
2496     {
2497         package.session().close();
2498     }
2499 }
2500
2501 std::string escape_cql_term(std::string inp)
2502 {
2503     std::string res;
2504     size_t l = inp.length();
2505     size_t i;
2506     for (i = 0; i < l; i++)
2507     {
2508         if (strchr("*?^\"", inp[i]))
2509             res += "\\";
2510         res += inp[i];
2511     }
2512     return res;
2513 }
2514
2515 void yf::Zoom::Frontend::auth(mp::Package &package, Z_InitRequest *req,
2516                               int *error, char **addinfo, ODR odr)
2517 {
2518     if (m_p->torus_auth_url.length() == 0)
2519         return;
2520
2521     std::string user;
2522     std::string password;
2523     if (req->idAuthentication)
2524     {
2525         Z_IdAuthentication *auth = req->idAuthentication;
2526         switch (auth->which)
2527         {
2528         case Z_IdAuthentication_open:
2529             if (auth->u.open)
2530             {
2531                 const char *cp = strchr(auth->u.open, '/');
2532                 if (cp)
2533                 {
2534                     user.assign(auth->u.open, cp - auth->u.open);
2535                     password.assign(cp + 1);
2536                 }
2537             }
2538             break;
2539         case Z_IdAuthentication_idPass:
2540             if (auth->u.idPass->userId)
2541                 user.assign(auth->u.idPass->userId);
2542             if (auth->u.idPass->password)
2543                 password.assign(auth->u.idPass->password);
2544             break;
2545         }
2546     }
2547
2548     std::string ip = package.origin().get_address();
2549     yaz_log(YLOG_LOG, "IP=%s", ip.c_str());
2550
2551     std::string torus_query;
2552     int failure_code;
2553
2554     if (user.length() && password.length())
2555     {
2556         torus_query = "userName==\"" + escape_cql_term(user) +
2557             "\" and password==\"" + escape_cql_term(password) + "\"";
2558         failure_code = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
2559     }
2560     else
2561     {
2562         const char *ip_cstr = ip.c_str();
2563         const char *cp = strchr(ip_cstr, ':');
2564         if (cp)
2565             ip_cstr = cp + 1;
2566
2567         torus_query = "ip encloses/net.ipaddress \"";
2568         torus_query += escape_cql_term(std::string(ip_cstr));
2569         torus_query += "\"";
2570         failure_code = YAZ_BIB1_INIT_AC_BLOCKED_NETWORK_ADDRESS;
2571     }
2572
2573     std::string dummy_db;
2574     std::string dummy_realm;
2575     xmlDoc *doc = mp::get_searchable(package, m_p->torus_auth_url, dummy_db,
2576                                      torus_query, dummy_realm, m_p->proxy);
2577     if (!doc)
2578     {
2579         // something fundamental broken in lookup.
2580         *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2581         *addinfo = odr_strdup(odr, "zoom: torus server unavailable or "
2582                               "incorrectly configured.");
2583         return;
2584     }
2585     const xmlNode *ptr = xmlDocGetRootElement(doc);
2586     if (ptr && ptr->type == XML_ELEMENT_NODE)
2587     {
2588         if (strcmp((const char *) ptr->name, "records") == 0)
2589         {
2590             ptr = ptr->children;
2591             while (ptr && ptr->type != XML_ELEMENT_NODE)
2592                 ptr = ptr->next;
2593         }
2594         if (ptr && strcmp((const char *) ptr->name, "record") == 0)
2595         {
2596             ptr = ptr->children;
2597             while (ptr && ptr->type != XML_ELEMENT_NODE)
2598                 ptr = ptr->next;
2599         }
2600         if (ptr && strcmp((const char *) ptr->name, "layer") == 0)
2601         {
2602             ptr = ptr->children;
2603             while (ptr && ptr->type != XML_ELEMENT_NODE)
2604                 ptr = ptr->next;
2605         }
2606         while (ptr)
2607         {
2608             if (ptr && ptr->type == XML_ELEMENT_NODE &&
2609                 !strcmp((const char *) ptr->name, "identityId"))
2610                 break;
2611             ptr = ptr->next;
2612         }
2613     }
2614     if (!ptr)
2615     {
2616         *error = failure_code;
2617         return;
2618     }
2619     session_realm = mp::xml::get_text(ptr);
2620 }
2621
2622 void yf::Zoom::Impl::process(mp::Package &package)
2623 {
2624     FrontendPtr f = get_frontend(package);
2625     Z_GDU *gdu = package.request().get();
2626
2627     if (f->m_is_virtual)
2628     {
2629         f->handle_package(package);
2630     }
2631     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
2632              Z_APDU_initRequest)
2633     {
2634         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
2635         f->m_init_gdu = gdu;
2636
2637         mp::odr odr;
2638         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
2639         Z_InitResponse *resp = apdu->u.initResponse;
2640
2641         int i;
2642         static const int masks[] = {
2643             Z_Options_search,
2644             Z_Options_present,
2645             -1
2646         };
2647         for (i = 0; masks[i] != -1; i++)
2648             if (ODR_MASK_GET(req->options, masks[i]))
2649                 ODR_MASK_SET(resp->options, masks[i]);
2650
2651         static const int versions[] = {
2652             Z_ProtocolVersion_1,
2653             Z_ProtocolVersion_2,
2654             Z_ProtocolVersion_3,
2655             -1
2656         };
2657         for (i = 0; versions[i] != -1; i++)
2658             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
2659                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
2660             else
2661                 break;
2662
2663         *resp->preferredMessageSize = *req->preferredMessageSize;
2664         *resp->maximumRecordSize = *req->maximumRecordSize;
2665
2666         int error = 0;
2667         char *addinfo = 0;
2668         f->auth(package, req, &error, &addinfo, odr);
2669         if (error)
2670         {
2671             resp->userInformationField =
2672                 zget_init_diagnostics(odr, error, addinfo);
2673             *resp->result = 0;
2674             package.session().close();
2675         }
2676         else
2677             f->m_is_virtual = true;
2678         package.response() = apdu;
2679     }
2680     else
2681         package.move();
2682
2683     release_frontend(package);
2684 }
2685
2686
2687 static mp::filter::Base* filter_creator()
2688 {
2689     return new mp::filter::Zoom;
2690 }
2691
2692 extern "C" {
2693     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
2694         0,
2695         "zoom",
2696         filter_creator
2697     };
2698 }
2699
2700
2701 /*
2702  * Local variables:
2703  * c-basic-offset: 4
2704  * c-file-style: "Stroustrup"
2705  * indent-tabs-mode: nil
2706  * End:
2707  * vim: shiftwidth=4 tabstop=8 expandtab
2708  */
2709