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 
37 #include <cstdlib>
38 #include <unistd.h>
39 #include <sys/types.h>
40 
41 #include "XrdSys/XrdSysError.hh"
42 #include "XrdSys/XrdSysPthread.hh"
44 #include "Xrd/XrdObject.hh"
46 #include "XrdOuc/XrdOucStream.hh"
47 #include "Xrd/XrdProtocol.hh"
48 #include "XrdOuc/XrdOucHash.hh"
51 #include "XrdNet/XrdNetPMark.hh"
52 
53 #include <openssl/ssl.h>
54 
55 #include <vector>
56 
57 #include "XrdHttpReq.hh"
58 
59 /******************************************************************************/
60 /* D e f i n e s */
61 /******************************************************************************/
62 
63 
64 #ifndef __GNUC__
65 #define __attribute__(x)
66 #endif
67 
68 class XrdOucTokenizer;
69 class XrdOucTrace;
70 class XrdBuffer;
71 class XrdLink;
72 class XrdXrootdProtocol;
73 class XrdHttpSecXtractor;
74 class XrdHttpExtHandler;
75 struct XrdVersionInfo;
76 class XrdOucGMap;
77 class XrdCryptoFactory;
78 
79 class XrdHttpProtocol : public XrdProtocol {
80 
81  friend class XrdHttpReq;
82  friend class XrdHttpExtReq;
83 
84 public:
85 
87  static int Configure(char *parms, XrdProtocol_Config *pi);
88 
90  void DoIt() {
91  if (Resume) (*this.*Resume)();
92  }
93 
96 
98  int Process(XrdLink *lp); // Sync: Job->Link.DoIt->Process
99 
100 
102  void Recycle(XrdLink *lp, int consec, const char *reason);
103 
105  int Stats(char *buff, int blen, int do_sync = 0);
106 
107 
108 
109 
111  int doStat(char *fname);
112 
114  int doChksum(const XrdOucString &fname);
115 
117  XrdHttpProtocol(const XrdHttpProtocol&) = default;
119  XrdHttpProtocol(bool imhttps);
121  Cleanup();
122  }
123 
126 
127 
130 
131  // XrdHttp checksum handling class
133 
136 
138  bool isHTTPS() { return ishttps; }
139 
140 private:
141 
142 
145 
147  static bool InitTLS();
148 
150  static bool InitSecurity();
151 
153  int StartSimpleResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
154 
156  int SendData(const char *body, int bodylen);
157 
159  void Cleanup();
160 
162  void Reset();
163 
166  int HandleAuthentication(XrdLink* lp);
167 
170  int GetVOMSData(XrdLink *lp);
171 
172  // Handle gridmap file mapping if present
173  // Second argument is the OpenSSL hash of the EEC, if present; this allows
174  // a consistent fallback if the user is not in the mapfile.
175  //
176  // @return 0 if successful, otherwise !0
177  int HandleGridMap(XrdLink* lp, const char * eechash);
178 
181  int getDataOneShot(int blen, bool wait=false);
182 
184  static BIO *CreateBIO(XrdLink *lp);
185 
189  struct extHInfo
190  {XrdOucString extHName; // The instance name (1 to 16 characters)
191  XrdOucString extHPath; // The shared library path
192  XrdOucString extHParm; // The parameter (sort of)
193 
194  extHInfo(const char *hName, const char *hPath, const char *hParm)
195  : extHName(hName), extHPath(hPath), extHParm(hParm) {}
197  };
199  static int Config(const char *fn, XrdOucEnv *myEnv);
200  static const char *Configed();
201  static int xtrace(XrdOucStream &Config);
202  static int xsslcert(XrdOucStream &Config);
203  static int xsslkey(XrdOucStream &Config);
204  static int xsecxtractor(XrdOucStream &Config);
205  static int xexthandler(XrdOucStream & Config, std::vector<extHInfo> &hiVec);
206  static int xsslcadir(XrdOucStream &Config);
207  static int xsslcipherfilter(XrdOucStream &Config);
208  static int xdesthttps(XrdOucStream &Config);
209  static int xlistdeny(XrdOucStream &Config);
210  static int xlistredir(XrdOucStream &Config);
211  static int xselfhttps2http(XrdOucStream &Config);
212  static int xembeddedstatic(XrdOucStream &Config);
213  static int xstaticredir(XrdOucStream &Config);
214  static int xstaticpreload(XrdOucStream &Config);
215  static int xgmap(XrdOucStream &Config);
216  static int xsslcafile(XrdOucStream &Config);
217  static int xsslverifydepth(XrdOucStream &Config);
218  static int xsecretkey(XrdOucStream &Config);
219  static int xheader2cgi(XrdOucStream &Config);
220  static int xhttpsmode(XrdOucStream &Config);
221  static int xtlsreuse(XrdOucStream &Config);
222  static int xtlsclientauth(XrdOucStream &Config);
224 
225  static bool isRequiredXtractor; // If true treat secxtractor errors as fatal
227 
228  static bool usingEC; // using XrdEC
229  // Loads the SecXtractor plugin, if available
230  static int LoadSecXtractor(XrdSysError *eDest, const char *libName,
231  const char *libParms);
232 
233  // An oldstyle struct array to hold exthandlers
234  #define MAX_XRDHTTPEXTHANDLERS 4
235  static struct XrdHttpExtHandlerInfo {
236  char name[16];
239  static int exthandlercnt;
240 
241  // Loads the ExtHandler plugin, if available
242  static int LoadExtHandler(std::vector<extHInfo> &hiVec,
243  const char *cFN, XrdOucEnv &myEnv);
244 
245  static int LoadExtHandler(XrdSysError *eDest, const char *libName,
246  const char *configFN, const char *libParms,
247  XrdOucEnv *myEnv, const char *instName);
248 
249  // Determines whether one of the loaded ExtHandlers are interested in
250  // handling a given request.
251  //
252  // Returns NULL if there is no matching handler.
254 
255  // Tells if an ext handler with the given name has already been loaded
256  static bool ExtHandlerLoaded(const char *handlername);
257 
262 
265 
267  int BuffAvailable();
269  int BuffUsed();
271  int BuffFree();
272 
274  void BuffConsume(int blen);
276  int BuffgetData(int blen, char **data, bool wait);
278  int BuffgetLine(XrdOucString &dest);
279 
281  int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen, bool keepalive);
282 
284  // API.
285  int StartChunkedResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
286 
288  // indicates that this is the last chunk in the response.
289  int ChunkResp(const char *body, long long bodylen);
290 
292  // of the chunk is known but the body is not immediately available.
293  int ChunkRespHeader(long long bodylen);
294 
296  int ChunkRespFooter();
297 
299  char *GetClientIPStr();
300 
303 
306 
309 
311  SSL *ssl;
312 
314  BIO *sbio;
315 
317  static BIO *sslbio_err;
318 
320  bool ishttps;
321 
324  bool ssldone;
325 
329 
333 
335 
336 protected:
337 
338  // Statistical area
339  //
340 // static XrdXrootdStats *SI;
341 // int numReads; // Count for kXR_read
342 // int numReadP; // Count for kXR_read pre-preads
343 // int numReadV; // Count for kR_readv
344 // int numSegsV; // Count for kR_readv segmens
345 // int numWrites; // Count
346 // int numFiles; // Count
347 //
348 // int cumReads; // Count less numReads
349 // int cumReadP; // Count less numReadP
350 // int cumReadV; // Count less numReadV
351 // int cumSegsV; // Count less numSegsV
352 // int cumWrites; // Count less numWrites
353 // long long totReadP; // Bytes
354 
355  static XrdScheduler *Sched; // System scheduler
356  static XrdBuffManager *BPool; // Buffer manager
357  static XrdSysError eDest; // Error message handler
358  static XrdSecService *CIA; // Authentication Server
359 
362 
365  char *Addr_str;
366 
368  static XrdOucGMap *servGMap; // Grid mapping service
369 
372 
373 
377 
378 
379  //
380  // Processing configuration values
381  //
382 
384  static int hailWait;
385 
387  static int readWait;
388 
390  static int Port;
391 
393  static char * Port_str;
394 
397 
399  static int crlRefIntervalSec;
400 
402  static char *gridmap;// [s] gridmap file [/etc/grid-security/gridmap]
403  static bool isRequiredGridmap; // If true treat gridmap errors as fatal
404  static bool compatNameGeneration; // If true, utilize the old algorithm for username generation for unknown users.
405 
407  static char *secretkey;
408 
410  static int sslverifydepth;
411 
413  static bool isdesthttps;
414 
416  static char *listredir;
417 
419  static bool listdeny;
420 
422  static bool selfhttps2http;
423 
425  static bool embeddedstatic;
426 
427  // Url to redirect to in the case a /static is requested
428  static char *staticredir;
429 
430  // Hash that keeps preloaded files
432  char *data;
433  int len;
434  };
436 
439 
441  static std::map< std::string, std::string > hdr2cgimap;
442 
444  static int m_bio_type;
445 
447  static BIO_METHOD *m_bio_method;
448 
450  static char * xrd_cslist;
451 
454 };
455 #endif
static char * secretkey
The key used to calculate the url hashes.
Definition: XrdHttpProtocol.hh:407
static XrdObjectQ< XrdHttpProtocol > ProtStack
Definition: XrdHttpProtocol.hh:124
bool postheaderauth
Definition: XrdHttpProtocol.hh:332
static int xgmap(XrdOucStream &Config)
char * Addr_str
Definition: XrdHttpProtocol.hh:365
static BIO * CreateBIO(XrdLink *lp)
Create a new BIO object from an XrdLink. Returns NULL on failure.
bool postheaderwait
Definition: XrdHttpProtocol.hh:328
static char * sslcafile
Definition: XrdHttpProtocol.hh:396
static int exthandlercnt
Definition: XrdHttpProtocol.hh:239
void Cleanup()
Deallocate resources, in order to reutilize an object of this class.
static bool isdesthttps
True if the redirections must be towards https targets.
Definition: XrdHttpProtocol.hh:413
static char * Port_str
Our port, as a string.
Definition: XrdHttpProtocol.hh:393
static int xheader2cgi(XrdOucStream &Config)
int(XrdHttpProtocol::* Resume)()
The resume function.
Definition: XrdHttpProtocol.hh:144
static kXR_int32 myRole
Our role.
Definition: XrdHttpProtocol.hh:438
static int xsecxtractor(XrdOucStream &Config)
static int readWait
Timeout for reading data.
Definition: XrdHttpProtocol.hh:387
static XrdHttpExtHandler * FindMatchingExtHandler(const XrdHttpReq &)
static char * sslcadir
Definition: XrdHttpProtocol.hh:396
char * GetClientIPStr()
Gets a string that represents the IP address of the client. Must be freed.
XrdSecEntity SecEntity
Authentication area.
Definition: XrdHttpProtocol.hh:129
static int Config(const char *fn, XrdOucEnv *myEnv)
Functions related to the configuration.
char * myBuffEnd
Definition: XrdHttpProtocol.hh:261
static int xsecretkey(XrdOucStream &Config)
static int xsslkey(XrdOucStream &Config)
Definition: XrdNetPMark.hh:38
Definition: XrdSecInterface.hh:553
int len
Definition: XrdHttpProtocol.hh:433
void BuffConsume(int blen)
Consume some bytes from the buffer.
Definition: XrdHttpReq.hh:71
int ChunkRespFooter()
Send the footer of the chunk response.
XrdHttpReq CurrentReq
Definition: XrdHttpProtocol.hh:376
static int xlistredir(XrdOucStream &Config)
int doStat(char *fname)
Perform a Stat request.
static bool compatNameGeneration
Definition: XrdHttpProtocol.hh:404
XrdHttpProtocol(const XrdHttpProtocol &)=default
Ctor, dtors and copy ctor.
static char * sslcipherfilter
Definition: XrdHttpProtocol.hh:396
static char * gridmap
Gridmap file location. The same used by XrdSecGsi.
Definition: XrdHttpProtocol.hh:402
int getDataOneShot(int blen, bool wait=false)
static int xstaticredir(XrdOucStream &Config)
Definition: XrdOucStream.hh:46
Definition: XrdHttpProtocol.hh:235
Definition: XrdXrootdBridge.hh:61
static int xhttpsmode(XrdOucStream &Config)
static bool listdeny
If true, any form of listing is denied.
Definition: XrdHttpProtocol.hh:419
int ChunkRespHeader(long long bodylen)
Send the beginning of a chunked response but not the body; useful when the size.
XrdBuffer * myBuff
Circular Buffer used to read the request.
Definition: XrdHttpProtocol.hh:259
int ChunkResp(const char *body, long long bodylen)
Send a (potentially partial) body in a chunked response; invoking with NULL body. ...
static XrdHttpReadRangeHandler::Configuration ReadRangeConfig
configuration for the read range handler
Definition: XrdHttpProtocol.hh:135
bool isHTTPS()
called via https
Definition: XrdHttpProtocol.hh:138
static int xstaticpreload(XrdOucStream &Config)
static bool InitTLS()
Initialization of the ssl security things.
XrdObject< XrdHttpProtocol > ProtLink
Definition: XrdHttpProtocol.hh:125
Definition: XrdProtocol.hh:124
static XrdHttpSecXtractor * secxtractor
Definition: XrdHttpProtocol.hh:226
char * myBuffStart
The circular pointers.
Definition: XrdHttpProtocol.hh:261
static bool ExtHandlerLoaded(const char *handlername)
Definition: XrdXrootdProtocol.hh:154
static bool isRequiredXtractor
Definition: XrdHttpProtocol.hh:225
static XrdOucHash< StaticPreloadInfo > * staticpreload
Definition: XrdHttpProtocol.hh:435
static const char * Configed()
Definition: XrdHttpChecksumHandler.hh:85
static bool isRequiredGridmap
Definition: XrdHttpProtocol.hh:403
Definition: XrdBuffer.hh:71
static BIO * sslbio_err
bio to print SSL errors
Definition: XrdHttpProtocol.hh:317
static XrdSecService * CIA
Definition: XrdHttpProtocol.hh:358
static int Configure(char *parms, XrdProtocol_Config *pi)
Read and apply the configuration.
Definition: XrdOucTrace.hh:35
Definition: XrdSysError.hh:89
int BuffUsed()
How many bytes in the buffer.
char * data
Definition: XrdHttpProtocol.hh:432
XrdHttpExtHandler * ptr
Definition: XrdHttpProtocol.hh:237
int BuffgetData(int blen, char **data, bool wait)
Get a pointer, valid for up to blen bytes from the buffer. Returns the validity.
BIO * sbio
Private SSL bio.
Definition: XrdHttpProtocol.hh:314
int BuffAvailable()
How many bytes still fit into the buffer in a contiguous way.
int doChksum(const XrdOucString &fname)
Perform a checksum request.
Definition: XrdScheduler.hh:45
static int Port
Our port.
Definition: XrdHttpProtocol.hh:390
static int sslverifydepth
Depth of verification of a certificate chain.
Definition: XrdHttpProtocol.hh:410
int SendData(const char *body, int bodylen)
Send some generic data to the client.
static int xtlsrequiredprefix(XrdOucStream &Config)
static int xsslcadir(XrdOucStream &Config)
static int xselfhttps2http(XrdOucStream &Config)
static XrdHttpChecksumHandler cksumHandler
Definition: XrdHttpProtocol.hh:132
static XrdScheduler * Sched
Definition: XrdHttpProtocol.hh:355
Definition: XrdHttpProtocol.hh:189
static int xsslverifydepth(XrdOucStream &Config)
int GetVOMSData(XrdLink *lp)
~extHInfo()
Definition: XrdHttpProtocol.hh:196
char name[16]
Definition: XrdHttpProtocol.hh:236
static int LoadSecXtractor(XrdSysError *eDest, const char *libName, const char *libParms)
XrdXrootd::Bridge * Bridge
The Bridge that we use to exercise the xrootd internals.
Definition: XrdHttpProtocol.hh:371
static bool selfhttps2http
If client is HTTPS, self-redirect with HTTP+token.
Definition: XrdHttpProtocol.hh:422
XrdProtocol * Match(XrdLink *lp)
Tells if the oustanding bytes on the socket match this protocol implementation.
Definition: XrdOucGMap.hh:48
~XrdHttpProtocol()
Definition: XrdHttpProtocol.hh:120
static int crlRefIntervalSec
CRL thread refresh interval.
Definition: XrdHttpProtocol.hh:399
static int xlistdeny(XrdOucStream &Config)
static XrdBuffManager * BPool
Definition: XrdHttpProtocol.hh:356
Definition: XrdProtocol.hh:55
Definition: XrdOucEnv.hh:41
#define MAX_XRDHTTPEXTHANDLERS
Definition: XrdHttpProtocol.hh:234
int HandleAuthentication(XrdLink *lp)
void DoIt()
Override from the base class.
Definition: XrdHttpProtocol.hh:90
static XrdOucGMap * servGMap
The instance of the DN mapper. Created only when a valid path is given.
Definition: XrdHttpProtocol.hh:368
XrdLink * Link
The link we are bound to.
Definition: XrdHttpProtocol.hh:361
Definition: XrdHttpExtHandler.hh:88
static int xsslcipherfilter(XrdOucStream &Config)
void Reset()
Reset values, counters, in order to reutilize an object of this class.
static int xexthandler(XrdOucStream &Config, std::vector< extHInfo > &hiVec)
static XrdNetPMark * pmarkHandle
Packet marking handler pointer (assigned from the environment during the Config() call) ...
Definition: XrdHttpProtocol.hh:453
XrdOucString extHName
Definition: XrdHttpProtocol.hh:190
static int hailWait
Timeout for reading the handshake.
Definition: XrdHttpProtocol.hh:384
bool ishttps
Tells if the client is https.
Definition: XrdHttpProtocol.hh:320
static std::map< std::string, std::string > hdr2cgimap
Rules that turn HTTP headers to cgi tokens in the URL, for internal comsumption.
Definition: XrdHttpProtocol.hh:441
Definition: XrdHttpReadRangeHandler.hh:54
static XrdSysError eDest
Definition: XrdHttpProtocol.hh:357
static int m_bio_type
Type identifier for our custom BIO objects.
Definition: XrdHttpProtocol.hh:444
Definition: XrdHttpProtocol.hh:79
int StartChunkedResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive)
Starts a chunked response; body of request is sent over multiple parts using the SendChunkResp.
static char * sslcert
OpenSSL stuff.
Definition: XrdHttpProtocol.hh:396
static char * xrd_cslist
The list of checksums that were configured via the xrd.cksum parameter on the server config file...
Definition: XrdHttpProtocol.hh:450
int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen, bool keepalive)
Sends a basic response. If the length is < 0 then it is calculated internally.
static bool InitSecurity()
Initialization fo security addon.
XrdOucString tmpline
A nice var to hold the current header line.
Definition: XrdHttpProtocol.hh:264
static BIO_METHOD * m_bio_method
C-style vptr table for our custom BIO objects.
Definition: XrdHttpProtocol.hh:447
static int xtlsclientauth(XrdOucStream &Config)
static XrdCryptoFactory * myCryptoFactory
Definition: XrdHttpProtocol.hh:334
static int LoadExtHandler(std::vector< extHInfo > &hiVec, const char *cFN, XrdOucEnv &myEnv)
long ResumeBytes
Tells that we are just waiting to have N bytes in the buffer.
Definition: XrdHttpProtocol.hh:308
int HandleGridMap(XrdLink *lp, const char *eechash)
static bool usingEC
Definition: XrdHttpProtocol.hh:228
Definition: XrdHttpSecXtractor.hh:41
SSL * ssl
Private SSL context.
Definition: XrdHttpProtocol.hh:311
int Stats(char *buff, int blen, int do_sync=0)
Get activity stats.
static char * listredir
Url to redirect to in the case a listing is requested.
Definition: XrdHttpProtocol.hh:416
Definition: XrdOucHash.hh:127
int StartSimpleResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive)
Start a response back to the client.
static char * staticredir
Definition: XrdHttpProtocol.hh:428
int kXR_int32
Definition: XPtypes.hh:89
bool DoneSetInfo
Indicates whether we&#39;ve attempted to send app info.
Definition: XrdHttpProtocol.hh:305
XrdOucString extHParm
Definition: XrdHttpProtocol.hh:192
Definition: XrdOucTokenizer.hh:32
Definition: XrdCryptoFactory.hh:121
int BuffFree()
How many bytes free in the buffer.
static bool embeddedstatic
If true, use the embedded css and icons.
Definition: XrdHttpProtocol.hh:425
Definition: XrdSecEntity.hh:64
static int xembeddedstatic(XrdOucStream &Config)
static int xtrace(XrdOucStream &Config)
Main request/response class, handling the logical status of the communication.
static char * sslkey
Definition: XrdHttpProtocol.hh:396
bool DoingLogin
Tells that we are just logging in.
Definition: XrdHttpProtocol.hh:302
int Process(XrdLink *lp)
Process data incoming from the socket.
XrdOucString extHPath
Definition: XrdHttpProtocol.hh:191
Definition: XrdHttpExtHandler.hh:47
bool ssldone
Definition: XrdHttpProtocol.hh:324
static int xsslcafile(XrdOucStream &Config)
Definition: XrdHttpProtocol.hh:431
int BuffgetLine(XrdOucString &dest)
Copy a full line of text from the buffer into dest. Zero if no line can be found in the buffer...
Definition: XrdOucString.hh:254
extHInfo(const char *hName, const char *hPath, const char *hParm)
Definition: XrdHttpProtocol.hh:194
void Recycle(XrdLink *lp, int consec, const char *reason)
Recycle this instance.
Definition: XrdBuffer.hh:41
static int xsslcert(XrdOucStream &Config)
static int xtlsreuse(XrdOucStream &Config)
static struct XrdHttpProtocol::XrdHttpExtHandlerInfo exthandler[MAX_XRDHTTPEXTHANDLERS]
static int xdesthttps(XrdOucStream &Config)
XrdHttpProtocol operator=(const XrdHttpProtocol &rhs)