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"
50 
51 #include <openssl/ssl.h>
52 
53 #include <vector>
54 
55 #include "XrdHttpReq.hh"
56 
57 /******************************************************************************/
58 /* D e f i n e s */
59 /******************************************************************************/
60 
61 
62 #ifndef __GNUC__
63 #define __attribute__(x)
64 #endif
65 
66 class XrdOucTokenizer;
67 class XrdOucTrace;
68 class XrdBuffer;
69 class XrdLink;
70 class XrdXrootdProtocol;
71 class XrdHttpSecXtractor;
72 class XrdHttpExtHandler;
73 struct XrdVersionInfo;
74 class XrdOucGMap;
75 class XrdCryptoFactory;
76 
77 class XrdHttpProtocol : public XrdProtocol {
78 
79  friend class XrdHttpReq;
80  friend class XrdHttpExtReq;
81 
82 public:
83 
85  static int Configure(char *parms, XrdProtocol_Config *pi);
86 
88  void DoIt() {
89  if (Resume) (*this.*Resume)();
90  }
91 
94 
96  int Process(XrdLink *lp); // Sync: Job->Link.DoIt->Process
97 
98 
100  void Recycle(XrdLink *lp, int consec, const char *reason);
101 
103  int Stats(char *buff, int blen, int do_sync = 0);
104 
105 
106 
107 
109  int doStat(char *fname);
110 
112  int doChksum(const XrdOucString &fname);
113 
115  XrdHttpProtocol(const XrdHttpProtocol&) = default;
117  XrdHttpProtocol(bool imhttps);
119  Cleanup();
120  }
121 
124 
125 
128 
129  // XrdHttp checksum handling class
131 
133  bool isHTTPS() { return ishttps; }
134 
135 private:
136 
137 
140 
142  static bool InitTLS();
143 
145  static bool InitSecurity();
146 
148  int StartSimpleResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
149 
151  int SendData(const char *body, int bodylen);
152 
154  void Cleanup();
155 
157  void Reset();
158 
161  int HandleAuthentication(XrdLink* lp);
162 
165  int GetVOMSData(XrdLink *lp);
166 
167  // Handle gridmap file mapping if present
168  // Second argument is the OpenSSL hash of the EEC, if present; this allows
169  // a consistent fallback if the user is not in the mapfile.
170  //
171  // @return 0 if successful, otherwise !0
172  int HandleGridMap(XrdLink* lp, const char * eechash);
173 
176  int getDataOneShot(int blen, bool wait=false);
177 
179  static BIO *CreateBIO(XrdLink *lp);
180 
184  struct extHInfo
185  {XrdOucString extHName; // The instance name (1 to 16 characters)
186  XrdOucString extHPath; // The shared library path
187  XrdOucString extHParm; // The parameter (sort of)
188 
189  extHInfo(const char *hName, const char *hPath, const char *hParm)
190  : extHName(hName), extHPath(hPath), extHParm(hParm) {}
192  };
194  static int Config(const char *fn, XrdOucEnv *myEnv);
195  static const char *Configed();
196  static int xtrace(XrdOucStream &Config);
197  static int xsslcert(XrdOucStream &Config);
198  static int xsslkey(XrdOucStream &Config);
199  static int xsecxtractor(XrdOucStream &Config);
200  static int xexthandler(XrdOucStream & Config, std::vector<extHInfo> &hiVec);
201  static int xsslcadir(XrdOucStream &Config);
202  static int xsslcipherfilter(XrdOucStream &Config);
203  static int xdesthttps(XrdOucStream &Config);
204  static int xlistdeny(XrdOucStream &Config);
205  static int xlistredir(XrdOucStream &Config);
206  static int xselfhttps2http(XrdOucStream &Config);
207  static int xembeddedstatic(XrdOucStream &Config);
208  static int xstaticredir(XrdOucStream &Config);
209  static int xstaticpreload(XrdOucStream &Config);
210  static int xgmap(XrdOucStream &Config);
211  static int xsslcafile(XrdOucStream &Config);
212  static int xsslverifydepth(XrdOucStream &Config);
213  static int xsecretkey(XrdOucStream &Config);
214  static int xheader2cgi(XrdOucStream &Config);
215  static int xhttpsmode(XrdOucStream &Config);
216  static int xtlsreuse(XrdOucStream &Config);
217 
218  static bool isRequiredXtractor; // If true treat secxtractor errors as fatal
220 
221  static bool usingEC; // using XrdEC
222  // Loads the SecXtractor plugin, if available
223  static int LoadSecXtractor(XrdSysError *eDest, const char *libName,
224  const char *libParms);
225 
226  // An oldstyle struct array to hold exthandlers
227  #define MAX_XRDHTTPEXTHANDLERS 4
228  static struct XrdHttpExtHandlerInfo {
229  char name[16];
232  static int exthandlercnt;
233 
234  // Loads the ExtHandler plugin, if available
235  static int LoadExtHandler(std::vector<extHInfo> &hiVec,
236  const char *cFN, XrdOucEnv &myEnv);
237 
238  static int LoadExtHandler(XrdSysError *eDest, const char *libName,
239  const char *configFN, const char *libParms,
240  XrdOucEnv *myEnv, const char *instName);
241 
242  // Determines whether one of the loaded ExtHandlers are interested in
243  // handling a given request.
244  //
245  // Returns NULL if there is no matching handler.
247 
248  // Tells if an ext handler with the given name has already been loaded
249  static bool ExtHandlerLoaded(const char *handlername);
250 
255 
258 
260  int BuffAvailable();
262  int BuffUsed();
264  int BuffFree();
265 
267  void BuffConsume(int blen);
269  int BuffgetData(int blen, char **data, bool wait);
271  int BuffgetLine(XrdOucString &dest);
272 
274  int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen, bool keepalive);
275 
277  // API.
278  int StartChunkedResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
279 
281  // indicates that this is the last chunk in the response.
282  int ChunkResp(const char *body, long long bodylen);
283 
285  // of the chunk is known but the body is not immediately available.
286  int ChunkRespHeader(long long bodylen);
287 
289  int ChunkRespFooter();
290 
292  char *GetClientIPStr();
293 
296 
299 
301  SSL *ssl;
302 
304  BIO *sbio;
305 
307  static BIO *sslbio_err;
308 
310  bool ishttps;
311 
314  bool ssldone;
316 
317 protected:
318 
319  // Statistical area
320  //
321 // static XrdXrootdStats *SI;
322 // int numReads; // Count for kXR_read
323 // int numReadP; // Count for kXR_read pre-preads
324 // int numReadV; // Count for kR_readv
325 // int numSegsV; // Count for kR_readv segmens
326 // int numWrites; // Count
327 // int numFiles; // Count
328 //
329 // int cumReads; // Count less numReads
330 // int cumReadP; // Count less numReadP
331 // int cumReadV; // Count less numReadV
332 // int cumSegsV; // Count less numSegsV
333 // int cumWrites; // Count less numWrites
334 // long long totReadP; // Bytes
335 
336  static XrdScheduler *Sched; // System scheduler
337  static XrdBuffManager *BPool; // Buffer manager
338  static XrdSysError eDest; // Error message handler
339  static XrdSecService *CIA; // Authentication Server
340 
343 
346  char *Addr_str;
347 
349  static XrdOucGMap *servGMap; // Grid mapping service
350 
353 
354 
358 
359 
360  //
361  // Processing configuration values
362  //
363 
365  static int hailWait;
366 
368  static int readWait;
369 
371  static int Port;
372 
374  static char * Port_str;
375 
378 
380  static int crlRefIntervalSec;
381 
383  static char *gridmap;// [s] gridmap file [/etc/grid-security/gridmap]
384  static bool isRequiredGridmap; // If true treat gridmap errors as fatal
385  static bool compatNameGeneration; // If true, utilize the old algorithm for username generation for unknown users.
386 
388  static char *secretkey;
389 
391  static int sslverifydepth;
392 
394  static bool isdesthttps;
395 
397  static char *listredir;
398 
400  static bool listdeny;
401 
403  static bool selfhttps2http;
404 
406  static bool embeddedstatic;
407 
408  // Url to redirect to in the case a /static is requested
409  static char *staticredir;
410 
411  // Hash that keeps preloaded files
413  char *data;
414  int len;
415  };
417 
420 
422  static std::map< std::string, std::string > hdr2cgimap;
423 
425  static int m_bio_type;
426 
428  static BIO_METHOD *m_bio_method;
429 
431  static char * xrd_cslist;
432 };
433 #endif
static char * secretkey
The key used to calculate the url hashes.
Definition: XrdHttpProtocol.hh:388
static XrdObjectQ< XrdHttpProtocol > ProtStack
Definition: XrdHttpProtocol.hh:122
static int xgmap(XrdOucStream &Config)
char * Addr_str
Definition: XrdHttpProtocol.hh:346
static BIO * CreateBIO(XrdLink *lp)
Create a new BIO object from an XrdLink. Returns NULL on failure.
static char * sslcafile
Definition: XrdHttpProtocol.hh:377
static int exthandlercnt
Definition: XrdHttpProtocol.hh:232
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:394
static char * Port_str
Our port, as a string.
Definition: XrdHttpProtocol.hh:374
static int xheader2cgi(XrdOucStream &Config)
int(XrdHttpProtocol::* Resume)()
The resume function.
Definition: XrdHttpProtocol.hh:139
static kXR_int32 myRole
Our role.
Definition: XrdHttpProtocol.hh:419
static int xsecxtractor(XrdOucStream &Config)
static int readWait
Timeout for reading data.
Definition: XrdHttpProtocol.hh:368
static XrdHttpExtHandler * FindMatchingExtHandler(const XrdHttpReq &)
static char * sslcadir
Definition: XrdHttpProtocol.hh:377
char * GetClientIPStr()
Gets a string that represents the IP address of the client. Must be freed.
XrdSecEntity SecEntity
Authentication area.
Definition: XrdHttpProtocol.hh:127
static int Config(const char *fn, XrdOucEnv *myEnv)
Functions related to the configuration.
char * myBuffEnd
Definition: XrdHttpProtocol.hh:254
static int xsecretkey(XrdOucStream &Config)
static int xsslkey(XrdOucStream &Config)
Definition: XrdSecInterface.hh:553
int len
Definition: XrdHttpProtocol.hh:414
void BuffConsume(int blen)
Consume some bytes from the buffer.
Definition: XrdHttpReq.hh:78
int ChunkRespFooter()
Send the footer of the chunk response.
XrdHttpReq CurrentReq
Definition: XrdHttpProtocol.hh:357
static int xlistredir(XrdOucStream &Config)
int doStat(char *fname)
Perform a Stat request.
static bool compatNameGeneration
Definition: XrdHttpProtocol.hh:385
XrdHttpProtocol(const XrdHttpProtocol &)=default
Ctor, dtors and copy ctor.
static char * sslcipherfilter
Definition: XrdHttpProtocol.hh:377
static char * gridmap
Gridmap file location. The same used by XrdSecGsi.
Definition: XrdHttpProtocol.hh:383
int getDataOneShot(int blen, bool wait=false)
static int xstaticredir(XrdOucStream &Config)
Definition: XrdOucStream.hh:46
Definition: XrdHttpProtocol.hh:228
Definition: XrdXrootdBridge.hh:61
static int xhttpsmode(XrdOucStream &Config)
static bool listdeny
If true, any form of listing is denied.
Definition: XrdHttpProtocol.hh:400
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:252
int ChunkResp(const char *body, long long bodylen)
Send a (potentially partial) body in a chunked response; invoking with NULL body. ...
bool isHTTPS()
called via https
Definition: XrdHttpProtocol.hh:133
static int xstaticpreload(XrdOucStream &Config)
static bool InitTLS()
Initialization of the ssl security things.
XrdObject< XrdHttpProtocol > ProtLink
Definition: XrdHttpProtocol.hh:123
Definition: XrdProtocol.hh:124
static XrdHttpSecXtractor * secxtractor
Definition: XrdHttpProtocol.hh:219
char * myBuffStart
The circular pointers.
Definition: XrdHttpProtocol.hh:254
static bool ExtHandlerLoaded(const char *handlername)
Definition: XrdXrootdProtocol.hh:154
static bool isRequiredXtractor
Definition: XrdHttpProtocol.hh:218
static XrdOucHash< StaticPreloadInfo > * staticpreload
Definition: XrdHttpProtocol.hh:416
static const char * Configed()
Definition: XrdHttpChecksumHandler.hh:85
static bool isRequiredGridmap
Definition: XrdHttpProtocol.hh:384
Definition: XrdBuffer.hh:71
static BIO * sslbio_err
bio to print SSL errors
Definition: XrdHttpProtocol.hh:307
static XrdSecService * CIA
Definition: XrdHttpProtocol.hh:339
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:413
XrdHttpExtHandler * ptr
Definition: XrdHttpProtocol.hh:230
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:304
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:371
static int sslverifydepth
Depth of verification of a certificate chain.
Definition: XrdHttpProtocol.hh:391
int SendData(const char *body, int bodylen)
Send some generic data to the client.
static int xsslcadir(XrdOucStream &Config)
static int xselfhttps2http(XrdOucStream &Config)
static XrdHttpChecksumHandler cksumHandler
Definition: XrdHttpProtocol.hh:130
static XrdScheduler * Sched
Definition: XrdHttpProtocol.hh:336
Definition: XrdHttpProtocol.hh:184
static int xsslverifydepth(XrdOucStream &Config)
int GetVOMSData(XrdLink *lp)
~extHInfo()
Definition: XrdHttpProtocol.hh:191
char name[16]
Definition: XrdHttpProtocol.hh:229
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:352
static bool selfhttps2http
If client is HTTPS, self-redirect with HTTP+token.
Definition: XrdHttpProtocol.hh:403
XrdProtocol * Match(XrdLink *lp)
Tells if the oustanding bytes on the socket match this protocol implementation.
Definition: XrdOucGMap.hh:48
~XrdHttpProtocol()
Definition: XrdHttpProtocol.hh:118
static int crlRefIntervalSec
CRL thread refresh interval.
Definition: XrdHttpProtocol.hh:380
static int xlistdeny(XrdOucStream &Config)
static XrdBuffManager * BPool
Definition: XrdHttpProtocol.hh:337
Definition: XrdProtocol.hh:55
Definition: XrdOucEnv.hh:41
#define MAX_XRDHTTPEXTHANDLERS
Definition: XrdHttpProtocol.hh:227
int HandleAuthentication(XrdLink *lp)
void DoIt()
Override from the base class.
Definition: XrdHttpProtocol.hh:88
static XrdOucGMap * servGMap
The instance of the DN mapper. Created only when a valid path is given.
Definition: XrdHttpProtocol.hh:349
XrdLink * Link
The link we are bound to.
Definition: XrdHttpProtocol.hh:342
Definition: XrdHttpExtHandler.hh:82
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)
XrdOucString extHName
Definition: XrdHttpProtocol.hh:185
static int hailWait
Timeout for reading the handshake.
Definition: XrdHttpProtocol.hh:365
bool ishttps
Tells if the client is https.
Definition: XrdHttpProtocol.hh:310
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:422
static XrdSysError eDest
Definition: XrdHttpProtocol.hh:338
static int m_bio_type
Type identifier for our custom BIO objects.
Definition: XrdHttpProtocol.hh:425
Definition: XrdHttpProtocol.hh:77
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:377
static char * xrd_cslist
The list of checksums that were configured via the xrd.cksum parameter on the server config file...
Definition: XrdHttpProtocol.hh:431
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:257
static BIO_METHOD * m_bio_method
C-style vptr table for our custom BIO objects.
Definition: XrdHttpProtocol.hh:428
static XrdCryptoFactory * myCryptoFactory
Definition: XrdHttpProtocol.hh:315
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:298
int HandleGridMap(XrdLink *lp, const char *eechash)
static bool usingEC
Definition: XrdHttpProtocol.hh:221
Definition: XrdHttpSecXtractor.hh:41
SSL * ssl
Private SSL context.
Definition: XrdHttpProtocol.hh:301
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:397
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:409
int kXR_int32
Definition: XPtypes.hh:89
XrdOucString extHParm
Definition: XrdHttpProtocol.hh:187
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:406
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:377
bool DoingLogin
Tells that we are just logging in.
Definition: XrdHttpProtocol.hh:295
int Process(XrdLink *lp)
Process data incoming from the socket.
XrdOucString extHPath
Definition: XrdHttpProtocol.hh:186
Definition: XrdHttpExtHandler.hh:45
bool ssldone
Definition: XrdHttpProtocol.hh:314
static int xsslcafile(XrdOucStream &Config)
Definition: XrdHttpProtocol.hh:412
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:189
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)