XRootD
XrdHttpProtocol.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of XrdHTTP: A pragmatic implementation of the
3 // HTTP/WebDAV protocol for the Xrootd framework
4 //
5 // Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
6 // Author: Fabrizio Furano <furano@cern.ch>
7 // File Date: Nov 2012
8 //------------------------------------------------------------------------------
9 // XRootD is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // XRootD is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public License
20 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
21 //------------------------------------------------------------------------------
22 
23 
24 #ifndef __XRDHTTP_PROTOCOL_H__
25 #define __XRDHTTP_PROTOCOL_H__
26 
36 #include "XrdSys/XrdSysError.hh"
37 #include "XrdSys/XrdSysPthread.hh"
39 #include "Xrd/XrdObject.hh"
41 #include "XrdOuc/XrdOucStream.hh"
42 #include "Xrd/XrdProtocol.hh"
43 #include "XrdOuc/XrdOucHash.hh"
46 #include "XrdNet/XrdNetPMark.hh"
48 #include "XrdHttpReq.hh"
49 
50 #include <chrono>
51 #include <cstdlib>
52 #include <openssl/ssl.h>
53 #include <sys/types.h>
54 #include <unistd.h>
55 #include <unordered_map>
56 #include <unordered_set>
57 #include <vector>
58 
59 
60 /******************************************************************************/
61 /* D e f i n e s */
62 /******************************************************************************/
63 
64 
65 #ifndef __GNUC__
66 #define __attribute__(x)
67 #endif
68 
69 class XrdOucTokenizer;
70 class XrdOucTrace;
71 class XrdBuffer;
72 class XrdLink;
73 class XrdHttpMon;
74 class XrdXrootdProtocol;
75 class XrdHttpSecXtractor;
76 class XrdHttpExtHandler;
77 struct XrdVersionInfo;
78 class XrdOucGMap;
79 class XrdCryptoFactory;
80 
81 class XrdHttpProtocol : public XrdProtocol {
82 
83  friend class XrdHttpReq;
84  friend class XrdHttpExtReq;
85 
86 public:
87 
89  static int Configure(char *parms, XrdProtocol_Config *pi);
90 
92  void DoIt() {
93  if (Resume) (*this.*Resume)();
94  }
95 
97  static int parseHeader2CGI(XrdOucStream &Config, XrdSysError & err, std::map<std::string, std::string> & header2cgi);
98 
100  XrdProtocol *Match(XrdLink *lp);
101 
103  int Process(XrdLink *lp); // Sync: Job->Link.DoIt->Process
104 
105 
107  void Recycle(XrdLink *lp, int consec, const char *reason);
108 
110  int Stats(char *buff, int blen, int do_sync = 0);
111 
112 
113 
114 
116  int doStat(char *fname);
117 
119  int doChksum(const XrdOucString &fname);
120 
122  XrdHttpProtocol(const XrdHttpProtocol&) = default;
124  XrdHttpProtocol(bool imhttps);
126  Cleanup();
127  }
128 
131 
132 
135 
136  // XrdHttp checksum handling class
138 
141 
143  bool isHTTPS() { return ishttps; }
144 
145 private:
146 
147 
149  int (XrdHttpProtocol::*Resume)();
150 
152  static bool InitTLS();
153 
155  static bool InitSecurity();
156 
158  int StartSimpleResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
159 
161  int SendData(const char *body, int bodylen);
162 
164  void Cleanup();
165 
167  void Reset();
168 
171  int HandleAuthentication(XrdLink* lp);
172 
175  int GetVOMSData(XrdLink *lp);
176 
177  // Handle gridmap file mapping if present
178  // Second argument is the OpenSSL hash of the EEC, if present; this allows
179  // a consistent fallback if the user is not in the mapfile.
180  //
181  // @return 0 if successful, otherwise !0
182  int HandleGridMap(XrdLink* lp, const char * eechash);
183 
186  int getDataOneShot(int blen, bool wait=false);
187 
189  static BIO *CreateBIO(XrdLink *lp);
190 
194  struct extHInfo
195  {XrdOucString extHName; // The instance name (1 to 16 characters)
196  XrdOucString extHPath; // The shared library path
197  XrdOucString extHParm; // The parameter (sort of)
198  bool extHNoTlsOK; // If true the exthandler can be loaded if TLS has NOT been configured
199  extHInfo(const char *hName, const char *hPath, const char *hParm, const bool hNoTlsOK)
200  : extHName(hName), extHPath(hPath), extHParm(hParm), extHNoTlsOK(hNoTlsOK) {}
201  ~extHInfo() {}
202  };
204  static int Config(const char *fn, XrdOucEnv *myEnv);
205  static const char *Configed();
206  static int xtrace(XrdOucStream &Config);
207  static int xsslcert(XrdOucStream &Config);
208  static int xsslkey(XrdOucStream &Config);
209  static int xsecxtractor(XrdOucStream &Config);
210  static int xcors(XrdOucStream &Config);
211  static int xexthandler(XrdOucStream & Config, std::vector<extHInfo> &hiVec);
212  static int xsslcadir(XrdOucStream &Config);
213  static int xsslcipherfilter(XrdOucStream &Config);
214  static int xdesthttps(XrdOucStream &Config);
215  static int xlistdeny(XrdOucStream &Config);
216  static int xlisting(XrdOucStream &Config);
217  static int xlistredir(XrdOucStream &Config);
218  static int xselfhttps2http(XrdOucStream &Config);
219  static int xembeddedstatic(XrdOucStream &Config);
220  static int xstaticheader(XrdOucStream &Config);
221  static int xstaticredir(XrdOucStream &Config);
222  static int xstaticpreload(XrdOucStream &Config);
223  static int xgmap(XrdOucStream &Config);
224  static int xsslcafile(XrdOucStream &Config);
225  static int xsslverifydepth(XrdOucStream &Config);
226  static int xsecretkey(XrdOucStream &Config);
227  static int xheader2cgi(XrdOucStream &Config);
228  static int xhttpsmode(XrdOucStream &Config);
229  static int xtlsreuse(XrdOucStream &Config);
230  static int xauth(XrdOucStream &Config);
231  static int xtlsclientauth(XrdOucStream &Config);
232  static int xmaxdelay(XrdOucStream &Config);
233 
234  static bool isRequiredXtractor; // If true treat secxtractor errors as fatal
235  static XrdHttpSecXtractor *secxtractor;
236 
237  static bool usingEC; // using XrdEC
238  static bool hasCache; // This is a caching server
239  // Loads the SecXtractor plugin, if available
240  static int LoadSecXtractor(XrdSysError *eDest, const char *libName,
241  const char *libParms);
242 
243  // An oldstyle struct array to hold exthandlers
244  #define MAX_XRDHTTPEXTHANDLERS 4
245  static struct XrdHttpExtHandlerInfo {
246  char name[16];
247  XrdHttpExtHandler *ptr;
248  } exthandler[MAX_XRDHTTPEXTHANDLERS];
249  static int exthandlercnt;
250 
251  static int LoadExtHandlerNoTls(std::vector<extHInfo> &hiVec,
252  const char *cFN, XrdOucEnv &myEnv);
253 
254  // Loads the ExtHandler plugin, if available
255  static int LoadExtHandler(std::vector<extHInfo> &hiVec,
256  const char *cFN, XrdOucEnv &myEnv);
257 
258  static int LoadExtHandler(XrdSysError *eDest, const char *libName,
259  const char *configFN, const char *libParms,
260  XrdOucEnv *myEnv, const char *instName);
261 
262  static int LoadCorsHandler(XrdSysError *eDest, const char * libname);
263  // Determines whether one of the loaded ExtHandlers are interested in
264  // handling a given request.
265  //
266  // Returns NULL if there is no matching handler.
267  static XrdHttpExtHandler *FindMatchingExtHandler(const XrdHttpReq &);
268 
269  // Tells if an ext handler with the given name has already been loaded
270  static bool ExtHandlerLoaded(const char *handlername);
271 
273  XrdBuffer *myBuff;
275  char *myBuffStart, *myBuffEnd;
276 
278  XrdOucString tmpline;
279 
281  int BuffAvailable();
283  int BuffUsed();
285  int BuffFree();
286 
288  void BuffConsume(int blen);
290  int BuffgetData(int blen, char **data, bool wait);
292  int BuffgetLine(XrdOucString &dest);
293 
295  int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen, bool keepalive);
296 
298  // API.
299  int StartChunkedResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
300 
302  // invoking with NULL body indicates that this is the last chunk in the response.
303  // invoking with bodylen=-1 indicates that this is a trailer
304  int ChunkResp(const char *body, long long bodylen);
305 
307  // of the chunk is known but the body is not immediately available.
308  int ChunkRespHeader(long long bodylen);
309 
311  int ChunkRespFooter();
312 
314  char *GetClientIPStr();
315 
317  bool DoingLogin;
318 
320  bool DoneSetInfo;
321 
323  long ResumeBytes;
324 
326  SSL *ssl;
327 
329  BIO *sbio;
330 
332  static BIO *sslbio_err;
333 
335  bool ishttps;
336 
339  bool ssldone;
340  static XrdCryptoFactory *myCryptoFactory;
341 
342 protected:
343 
344  // Statistical area
345  //
346 // static XrdXrootdStats *SI;
347 // int numReads; // Count for kXR_read
348 // int numReadP; // Count for kXR_read pre-preads
349 // int numReadV; // Count for kR_readv
350 // int numSegsV; // Count for kR_readv segmens
351 // int numWrites; // Count
352 // int numFiles; // Count
353 //
354 // int cumReads; // Count less numReads
355 // int cumReadP; // Count less numReadP
356 // int cumReadV; // Count less numReadV
357 // int cumSegsV; // Count less numSegsV
358 // int cumWrites; // Count less numWrites
359 // long long totReadP; // Bytes
360 
361  static XrdScheduler *Sched; // System scheduler
362  static XrdBuffManager *BPool; // Buffer manager
363  static XrdSysError eDest; // Error message handler
364  static XrdSecService *CIA; // Authentication Server
365 
368 
371  char *Addr_str;
372 
374  static XrdOucGMap *servGMap; // Grid mapping service
375 
378 
379 
383 
384  static std::string xrdcorsLibPath;
385 
387  //
388  // Processing configuration values
389  //
390 
392  static int hailWait;
393 
395  static int readWait;
396 
398  static int Port;
399 
401  static char * Port_str;
402 
405 
407  static int crlRefIntervalSec;
408 
409  // Allows missing CRL for CA verification
410  static bool allowMissingCRL;
411 
413  static char *gridmap;// [s] gridmap file [/etc/grid-security/gridmap]
414  static bool isRequiredGridmap; // If true treat gridmap errors as fatal
415  static bool compatNameGeneration; // If true, utilize the old algorithm for username generation for unknown users.
416 
418  static char *secretkey;
419 
421  static int sslverifydepth;
422 
424  static bool isdesthttps;
425 
427  static char *listredir;
428 
430  static bool listdeny;
431 
433  static bool selfhttps2http;
434 
436  static std::unordered_set<std::string> strp_cgi_params;
437 
439  static bool embeddedstatic;
440 
441  // Url to redirect to in the case a /static is requested
442  static char *staticredir;
443 
444  // Maximum amount of time an operation on the bridge can be
445  // delayed
446  static int m_maxdelay;
447 
448  // Hash that keeps preloaded files
450  char *data;
451  int len;
452  };
454 
457 
459  static std::map< std::string, std::string > hdr2cgimap;
460 
462  static int m_bio_type;
463 
465  static BIO_METHOD *m_bio_method;
466 
468  static char * xrd_cslist;
469 
472 
474  static bool tpcForwardCreds;
475 
477  static std::unordered_map<std::string, std::vector<std::pair<std::string, std::string>>> m_staticheader_map;
478 
481  static std::unordered_map<std::string, std::string> m_staticheaders;
482 };
483 #endif
int kXR_int32
Definition: XPtypes.hh:89
#define MAX_XRDHTTPEXTHANDLERS
Main request/response class, handling the logical status of the communication.
int StartSimpleResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive)
int SendData(const char *body, int bodylen)
static char * secretkey
The key used to calculate the url hashes.
static BIO_METHOD * m_bio_method
C-style vptr table for our custom BIO objects.
static char * gridmap
Gridmap file location. The same used by XrdSecGsi.
static XrdScheduler * Sched
static kXR_int32 myRole
Our role.
static char * sslcafile
static XrdNetPMark * pmarkHandle
Packet marking handler pointer (assigned from the environment during the Config() call)
static char * Port_str
Our port, as a string.
XrdXrootd::Bridge * Bridge
The Bridge that we use to exercise the xrootd internals.
static char * staticredir
static XrdSysError eDest
static bool selfhttps2http
If client is HTTPS, self-redirect with HTTP+token.
static XrdHttpChecksumHandler cksumHandler
static XrdOucGMap * servGMap
The instance of the DN mapper. Created only when a valid path is given.
static int hailWait
Timeout for reading the handshake.
int doChksum(const XrdOucString &fname)
Perform a checksum request.
static int m_maxdelay
static XrdOucHash< StaticPreloadInfo > * staticpreload
static char * xrd_cslist
The list of checksums that were configured via the xrd.cksum parameter on the server config file.
static char * sslcipherfilter
static int m_bio_type
Type identifier for our custom BIO objects.
static std::map< std::string, std::string > hdr2cgimap
Rules that turn HTTP headers to cgi tokens in the URL, for internal comsumption.
static char * sslcert
OpenSSL stuff.
XrdLink * Link
The link we are bound to.
static char * sslkey
int doStat(char *fname)
Perform a Stat request.
XrdObject< XrdHttpProtocol > ProtLink
static int readWait
Timeout for reading data.
void Recycle(XrdLink *lp, int consec, const char *reason)
Recycle this instance.
void DoIt()
Override from the base class.
static char * sslcadir
XrdHttpProtocol operator=(const XrdHttpProtocol &rhs)
bool isHTTPS()
called via https
static XrdHttpCors * xrdcors
static bool compatNameGeneration
static std::string xrdcorsLibPath
static bool allowMissingCRL
static bool isdesthttps
True if the redirections must be towards https targets.
static XrdObjectQ< XrdHttpProtocol > ProtStack
XrdProtocol * Match(XrdLink *lp)
Tells if the oustanding bytes on the socket match this protocol implementation.
static std::unordered_map< std::string, std::vector< std::pair< std::string, std::string > > > m_staticheader_map
The static headers to always return; map is from verb to a list of (header, val) pairs.
static bool isRequiredGridmap
static std::unordered_set< std::string > strp_cgi_params
CGI parameters (names) to strip from redirect URLs.
static char * listredir
Url to redirect to in the case a listing is requested.
int Stats(char *buff, int blen, int do_sync=0)
Get activity stats.
static std::unordered_map< std::string, std::string > m_staticheaders
XrdHttpReq CurrentReq
static int crlRefIntervalSec
CRL thread refresh interval.
static int Port
Our port.
static XrdHttpReadRangeHandler::Configuration ReadRangeConfig
configuration for the read range handler
static XrdSecService * CIA
static XrdBuffManager * BPool
static bool tpcForwardCreds
If set to true, the HTTP TPC transfers will forward the credentials to redirected hosts.
int Process(XrdLink *lp)
Process data incoming from the socket.
XrdHttpProtocol(const XrdHttpProtocol &)=default
Ctor, dtors and copy ctor.
static bool listdeny
If true, any form of listing is denied.
static int parseHeader2CGI(XrdOucStream &Config, XrdSysError &err, std::map< std::string, std::string > &header2cgi)
Use this function to parse header2cgi configurations.
XrdSecEntity SecEntity
Authentication area.
static bool embeddedstatic
If true, use the embedded css and icons.
static int sslverifydepth
Depth of verification of a certificate chain.
static int Configure(char *parms, XrdProtocol_Config *pi)
Read and apply the configuration.
bool InitTLS()
Definition: XrdClTls.cc:96
XrdCmsConfig Config