Fixed bug #189: Bad authenticated session transfer.
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* $Id: proxy.h,v 1.8 2004-10-23 23:12:24 adam Exp $
2    Copyright (c) 1998-2004, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <yaz++/z-assoc.h>
23 #include <yaz++/z-query.h>
24 #include <yaz++/z-databases.h>
25 #include <yaz++/cql2rpn.h>
26 #include <yaz/cql.h>
27 #include <yazproxy/bw.h>
28
29 class Yaz_Proxy;
30
31 #define MAX_ZURL_PLEX 10
32
33 #define PROXY_LOG_APDU_CLIENT 1
34 #define PROXY_LOG_APDU_SERVER 2
35 #define PROXY_LOG_REQ_CLIENT 4
36 #define PROXY_LOG_REQ_SERVER 8
37
38 struct Yaz_RecordCache_Entry;
39 class Yaz_ProxyConfigP;
40
41 class YAZ_EXPORT Yaz_ProxyConfig {
42 public:
43     Yaz_ProxyConfig();
44     ~Yaz_ProxyConfig();
45     int read_xml(const char *fname);
46
47     int get_target_no(int no,
48                       const char **name,
49                       const char **url,
50                       int *limit_bw,
51                       int *limit_pdu,
52                       int *limit_req,
53                       int *target_idletime,
54                       int *client_idletime,
55                       int *max_clients,
56                       int *keepalive_limit_bw,
57                       int *keepalive_limit_pdu,
58                       int *pre_init,
59                       const char **cql2rpn,
60                       const char **authentication);
61     
62     void get_generic_info(int *log_mask, int *max_clients);
63
64     void get_target_info(const char *name, const char **url,
65                          int *limit_bw, int *limit_pdu, int *limit_req,
66                          int *target_idletime, int *client_idletime,
67                          int *max_clients,
68                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
69                          int *pre_init,
70                          const char **cql2rpn,
71                          const char **authentication);
72
73     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
74     int check_syntax(ODR odr, const char *name,
75                      Odr_oid *syntax, Z_RecordComposition *comp,
76                      char **addinfo, char **stylesheet, char **schema,
77                      char **backend_type, char **backend_charset);
78     char *get_explain_doc(ODR odr, const char *name, const char *db,
79                           int *len);
80     const char *get_explain_name(const char *db, const char **backend_db);
81  private:
82     void operator=(const Yaz_ProxyConfig &conf);
83     class Yaz_ProxyConfigP *m_cp;
84 };
85
86 class YAZ_EXPORT Yaz_RecordCache {
87  public:
88     Yaz_RecordCache ();
89     ~Yaz_RecordCache ();
90     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
91     
92     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
93                 Odr_oid *syntax, Z_RecordComposition *comp);
94     void clear();
95
96     void copy_searchRequest(Z_SearchRequest *sr);
97     void copy_presentRequest(Z_PresentRequest *pr);
98     void set_max_size(int sz);
99  private:
100     NMEM m_mem;
101     Yaz_RecordCache_Entry *m_entries;
102     Z_SearchRequest *m_searchRequest;
103     Z_PresentRequest *m_presentRequest;
104     int match (Yaz_RecordCache_Entry *entry,
105                Odr_oid *syntax, int offset,
106                Z_RecordComposition *comp);
107     int m_max_size;
108 };
109
110 /// Private class
111 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
112     friend class Yaz_Proxy;
113     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
114                     Yaz_Proxy *parent);
115     ~Yaz_ProxyClient();
116     void recv_GDU(Z_GDU *apdu, int len);
117     void recv_Z_PDU(Z_APDU *apdu, int len);
118     void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
119     IYaz_PDU_Observer* sessionNotify
120         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
121     void shutdown();
122     Yaz_Proxy *m_server;
123     void failNotify();
124     void timeoutNotify();
125     void connectNotify();
126     int send_to_target(Z_APDU *apdu);
127     const char *get_session_str();
128     char *m_cookie;
129     Yaz_ProxyClient *m_next;
130     Yaz_ProxyClient **m_prev;
131     int m_init_flag;
132     Yaz_Z_Query *m_last_query;
133     Yaz_Z_Databases m_last_databases;
134     char *m_last_resultSetId;
135     int m_last_ok;
136     int m_last_resultCount;
137     int m_sr_transform;
138     int m_seqno;
139     int m_waiting;
140     int m_resultSetStartPoint;
141     int m_bytes_sent;
142     int m_bytes_recv;
143     int m_pdu_recv;
144     ODR m_init_odr;
145     Z_APDU *m_initResponse;
146     Z_Options *m_initResponse_options;
147     Z_ProtocolVersion *m_initResponse_version;
148     int m_initResponse_preferredMessageSize;
149     int m_initResponse_maximumRecordSize;
150     Yaz_RecordCache m_cache;
151     void pre_init_client();
152     int m_target_idletime;
153     Yaz_Proxy *m_root;
154 };
155
156
157 /// Information Retrieval Proxy Server.
158 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
159  private:
160     char *get_cookie(Z_OtherInformation **otherInfo);
161     char *get_proxy(Z_OtherInformation **otherInfo);
162     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
163                                 const char *proxy_host);
164     void srw_get_client(const char *db, const char **backend_db);
165     Z_APDU *result_set_optimize(Z_APDU *apdu);
166     void shutdown();
167     void releaseClient();    
168     Yaz_ProxyClient *m_client;
169     IYaz_PDU_Observable *m_PDU_Observable;
170     Yaz_ProxyClient *m_clientPool;
171     Yaz_Proxy *m_parent;
172     int m_seqno;
173     int m_max_clients;
174     int m_log_mask;
175     int m_keepalive_limit_bw;
176     int m_keepalive_limit_pdu;
177     int m_client_idletime;
178     int m_target_idletime;
179     char *m_proxyTarget;
180     char *m_default_target;
181     char *m_proxy_authentication;
182     long m_seed;
183     char *m_optimize;
184     int m_session_no;         // sequence for each client session
185     char m_session_str[30];  // session string (time:session_no)
186     Yaz_ProxyConfig *m_config;
187     char *m_config_fname;
188     int m_bytes_sent;
189     int m_bytes_recv;
190     int m_bw_max;
191     Yaz_bw m_bw_stat;
192     int m_pdu_max;
193     Yaz_bw m_pdu_stat;
194     Z_GDU *m_bw_hold_PDU;
195     int m_max_record_retrieve;
196     void handle_max_record_retrieve(Z_APDU *apdu);
197     void display_diagrecs(Z_DiagRec **pp, int num);
198     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
199                                               const char *addinfo);
200
201     Z_APDU *handle_query_validation(Z_APDU *apdu);
202     Z_APDU *handle_query_transformation(Z_APDU *apdu);
203
204     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
205     const char *load_balance(const char **url);
206     int m_reconfig_flag;
207     Yaz_ProxyConfig *check_reconfigure();
208     int m_request_no;
209     int m_invalid_session;
210     int m_marcxml_flag;
211     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr 
212     int m_stylesheet_offset;
213     Z_APDU *m_stylesheet_apdu;
214     Z_NamePlusRecordList *m_stylesheet_nprl;
215     char *m_schema;
216     char *m_backend_type;
217     char *m_backend_charset;
218     int m_frontend_type;
219     void convert_to_frontend_type(Z_NamePlusRecordList *p);
220     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
221     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
222     void convert_xsl_delay();
223     Z_APDU *m_initRequest_apdu;
224     int m_initRequest_preferredMessageSize;
225     int m_initRequest_maximumRecordSize;
226     Z_Options *m_initRequest_options;
227     Z_ProtocolVersion *m_initRequest_version;
228     NMEM m_initRequest_mem;
229     Z_APDU *m_apdu_invalid_session;
230     NMEM m_mem_invalid_session;
231     int send_PDU_convert(Z_APDU *apdu);
232     ODR m_s2z_odr_init;
233     ODR m_s2z_odr_search;
234     int m_s2z_hit_count;
235     int m_s2z_packing;
236     char *m_s2z_database;
237     Z_APDU *m_s2z_init_apdu;
238     Z_APDU *m_s2z_search_apdu;
239     Z_APDU *m_s2z_present_apdu;
240     char *m_s2z_stylesheet;
241     char *m_soap_ns;
242     int send_to_srw_client_error(int error, const char *add);
243     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
244     int send_http_response(int code);
245     int send_srw_response(Z_SRW_PDU *srw_pdu);
246     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
247                                   int num_diagnostics);
248     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
249                       Z_DefaultDiagFormat *ddf);
250     int m_http_keepalive;
251     const char *m_http_version;
252     Yaz_cql2rpn m_cql2rpn;
253     void *m_time_tv;
254     void logtime();
255     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
256     Z_ReferenceId *m_referenceId;
257     NMEM m_referenceId_mem;
258 #define NO_SPARE_SOLARIS_FD 10
259     int m_lo_fd[NO_SPARE_SOLARIS_FD];
260     void low_socket_open();
261     void low_socket_close();
262  public:
263     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
264               Yaz_Proxy *parent = 0);
265     ~Yaz_Proxy();
266     void inc_request_no();
267     void recv_GDU(Z_GDU *apdu, int len);
268     void handle_incoming_HTTP(Z_HTTP_Request *req);
269     void handle_incoming_Z_PDU(Z_APDU *apdu);
270     IYaz_PDU_Observer* sessionNotify
271         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
272     void failNotify();
273     void timeoutNotify();
274     void connectNotify();
275     void markInvalid();
276     const char *option(const char *name, const char *value);
277     void set_default_target(const char *target);
278     void set_proxy_authentication (const char *auth);
279     char *get_proxy_target() { return m_proxyTarget; };
280     char *get_session_str() { return m_session_str; };
281     void set_max_clients(int m) { m_max_clients = m; };
282     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
283     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
284     int get_target_idletime () { return m_target_idletime; }
285     int set_config(const char *name);
286     void reconfig() { m_reconfig_flag = 1; }
287     int send_to_client(Z_APDU *apdu);
288     int server(const char *addr);
289     void pre_init();
290     int get_log_mask() { return m_log_mask; };
291     int handle_init_response_for_invalid_session(Z_APDU *apdu);
292 };
293