xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"
49 
50 #include <openssl/ssl.h>
51 
52 #include <vector>
53 
54 #include "XrdHttpReq.hh"
55 
56 /******************************************************************************/
57 /* D e f i n e s */
58 /******************************************************************************/
59 
60 
61 #ifndef __GNUC__
62 #define __attribute__(x)
63 #endif
64 
65 class XrdOucTokenizer;
66 class XrdOucTrace;
67 class XrdBuffer;
68 class XrdLink;
69 class XrdXrootdProtocol;
70 class XrdHttpSecXtractor;
71 class XrdHttpExtHandler;
72 struct XrdVersionInfo;
73 class XrdOucGMap;
74 class XrdCryptoFactory;
75 
76 class XrdHttpProtocol : public XrdProtocol {
77 
78  friend class XrdHttpReq;
79  friend class XrdHttpExtReq;
80 
81 public:
82 
84  static int Configure(char *parms, XrdProtocol_Config *pi);
85 
87  void DoIt() {
88  if (Resume) (*this.*Resume)();
89  }
90 
93 
95  int Process(XrdLink *lp); // Sync: Job->Link.DoIt->Process
96 
97 
99  void Recycle(XrdLink *lp, int consec, const char *reason);
100 
102  int Stats(char *buff, int blen, int do_sync = 0);
103 
104 
105 
106 
108  int doStat(char *fname);
109 
111  int doChksum(const XrdOucString &fname);
112 
114  XrdHttpProtocol(const XrdHttpProtocol&) = default;
116  XrdHttpProtocol(bool imhttps);
118  Cleanup();
119  }
120 
123 
124 
127 
129  bool isHTTPS() { return ishttps; }
130 
131 private:
132 
133 
136 
138  static bool InitTLS();
139 
141  static bool InitSecurity();
142 
144  int StartSimpleResp(int code, const char *desc, const char *header_to_add, long long bodylen, bool keepalive);
145 
147  int SendData(const char *body, int bodylen);
148 
150  void Cleanup();
151 
153  void Reset();
154 
157  int HandleAuthentication(XrdLink* lp);
158 
161  int GetVOMSData(XrdLink *lp);
162 
163  // Handle gridmap file mapping if present
164  //
165  // @return 0 if successful, otherwise !0
166  int HandleGridMap(XrdLink* lp);
167 
170  int getDataOneShot(int blen, bool wait=false);
171 
173  static BIO *CreateBIO(XrdLink *lp);
174 
178  struct extHInfo
179  {XrdOucString extHName; // The instance name (1 to 16 characters)
180  XrdOucString extHPath; // The shared library path
181  XrdOucString extHParm; // The parameter (sort of)
182 
183  extHInfo(const char *hName, const char *hPath, const char *hParm)
184  : extHName(hName), extHPath(hPath), extHParm(hParm) {}
186  };
188  static int Config(const char *fn, XrdOucEnv *myEnv);
189  static const char *Configed();
190  static int xtrace(XrdOucStream &Config);
191  static int xsslcert(XrdOucStream &Config);
192  static int xsslkey(XrdOucStream &Config);
193  static int xsecxtractor(XrdOucStream &Config);
194  static int xexthandler(XrdOucStream & Config, std::vector<extHInfo> &hiVec);
195  static int xsslcadir(XrdOucStream &Config);
196  static int xsslcipherfilter(XrdOucStream &Config);
197  static int xdesthttps(XrdOucStream &Config);
198  static int xlistdeny(XrdOucStream &Config);
199  static int xlistredir(XrdOucStream &Config);
200  static int xselfhttps2http(XrdOucStream &Config);
201  static int xembeddedstatic(XrdOucStream &Config);
202  static int xstaticredir(XrdOucStream &Config);
203  static int xstaticpreload(XrdOucStream &Config);
204  static int xgmap(XrdOucStream &Config);
205  static int xsslcafile(XrdOucStream &Config);
206  static int xsslverifydepth(XrdOucStream &Config);
207  static int xsecretkey(XrdOucStream &Config);
208  static int xheader2cgi(XrdOucStream &Config);
209  static int xhttpsmode(XrdOucStream &Config);
210  static int xtlsreuse(XrdOucStream &Config);
211 
212  static bool isRequiredXtractor; // If true treat secxtractor errors as fatal
214 
215  // Loads the SecXtractor plugin, if available
216  static int LoadSecXtractor(XrdSysError *eDest, const char *libName,
217  const char *libParms);
218 
219  // An oldstyle struct array to hold exthandlers
220  #define MAX_XRDHTTPEXTHANDLERS 4
221  static struct XrdHttpExtHandlerInfo {
222  char name[16];
225  static int exthandlercnt;
226 
227  // Loads the ExtHandler plugin, if available
228  static int LoadExtHandler(std::vector<extHInfo> &hiVec,
229  const char *cFN, XrdOucEnv &myEnv);
230 
231  static int LoadExtHandler(XrdSysError *eDest, const char *libName,
232  const char *configFN, const char *libParms,
233  XrdOucEnv *myEnv, const char *instName);
234 
235  // Determines whether one of the loaded ExtHandlers are interested in
236  // handling a given request.
237  //
238  // Returns NULL if there is no matching handler.
240 
241  // Tells if an ext handler with the given name has already been loaded
242  static bool ExtHandlerLoaded(const char *handlername);
243 
248 
251 
253  int BuffAvailable();
255  int BuffUsed();
257  int BuffFree();
258 
260  void BuffConsume(int blen);
262  int BuffgetData(int blen, char **data, bool wait);
264  int BuffgetLine(XrdOucString &dest);
265 
267  int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen, bool keepalive);
268 
270  // API.
271  int StartChunkedResp(int code, const char *desc, const char *header_to_add, bool keepalive);
272 
274  // indicates that this is the last chunk in the response.
275  int ChunkResp(const char *body, long long bodylen);
276 
278  char *GetClientIPStr();
279 
282 
285 
287  SSL *ssl;
288 
290  BIO *sbio;
291 
293  static BIO *sslbio_err;
294 
296  bool ishttps;
297 
300  bool ssldone;
302 
303 protected:
304 
305  // Statistical area
306  //
307 // static XrdXrootdStats *SI;
308 // int numReads; // Count for kXR_read
309 // int numReadP; // Count for kXR_read pre-preads
310 // int numReadV; // Count for kR_readv
311 // int numSegsV; // Count for kR_readv segmens
312 // int numWrites; // Count
313 // int numFiles; // Count
314 //
315 // int cumReads; // Count less numReads
316 // int cumReadP; // Count less numReadP
317 // int cumReadV; // Count less numReadV
318 // int cumSegsV; // Count less numSegsV
319 // int cumWrites; // Count less numWrites
320 // long long totReadP; // Bytes
321 
322  static XrdScheduler *Sched; // System scheduler
323  static XrdBuffManager *BPool; // Buffer manager
324  static XrdSysError eDest; // Error message handler
325  static XrdSecService *CIA; // Authentication Server
326 
329 
332  char *Addr_str;
333 
335  static XrdOucGMap *servGMap; // Grid mapping service
336 
339 
340 
344 
345 
346  //
347  // Processing configuration values
348  //
349 
351  static int hailWait;
352 
354  static int readWait;
355 
357  static int Port;
358 
360  static char * Port_str;
361 
364 
366  static char *gridmap;// [s] gridmap file [/etc/grid-security/gridmap]
367  static bool isRequiredGridmap; // If true treat gridmap errors as fatal
368 
370  static char *secretkey;
371 
373  static int sslverifydepth;
374 
376  static bool isdesthttps;
377 
379  static char *listredir;
380 
382  static bool listdeny;
383 
385  static bool selfhttps2http;
386 
388  static bool embeddedstatic;
389 
390  // Url to redirect to in the case a /static is requested
391  static char *staticredir;
392 
393  // Hash that keeps preloaded files
395  char *data;
396  int len;
397  };
399 
402 
404  static std::map< std::string, std::string > hdr2cgimap;
405 
407  static int m_bio_type;
408 
410  static BIO_METHOD *m_bio_method;
411 };
412 #endif
static char * secretkey
The key used to calculate the url hashes.
Definition: XrdHttpProtocol.hh:370
static XrdObjectQ< XrdHttpProtocol > ProtStack
Definition: XrdHttpProtocol.hh:121
static int xgmap(XrdOucStream &Config)
char * Addr_str
Definition: XrdHttpProtocol.hh:332
static BIO * CreateBIO(XrdLink *lp)
Create a new BIO object from an XrdLink. Returns NULL on failure.
int(XrdHttpProtocol::* Resume)()
The resume function.
Definition: XrdHttpProtocol.hh:135
static char * sslcafile
Definition: XrdHttpProtocol.hh:363
static int exthandlercnt
Definition: XrdHttpProtocol.hh:225
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:376
static char * Port_str
Our port, as a string.
Definition: XrdHttpProtocol.hh:360
static int xheader2cgi(XrdOucStream &Config)
static kXR_int32 myRole
Our role.
Definition: XrdHttpProtocol.hh:401
static int xsecxtractor(XrdOucStream &Config)
static int readWait
Timeout for reading data.
Definition: XrdHttpProtocol.hh:354
static XrdHttpExtHandler * FindMatchingExtHandler(const XrdHttpReq &)
static char * sslcadir
Definition: XrdHttpProtocol.hh:363
char * GetClientIPStr()
Gets a string that represents the IP address of the client. Must be freed.
XrdSecEntity SecEntity
Authentication area.
Definition: XrdHttpProtocol.hh:126
static int Config(const char *fn, XrdOucEnv *myEnv)
Functions related to the configuration.
char * myBuffEnd
Definition: XrdHttpProtocol.hh:247
static int xsecretkey(XrdOucStream &Config)
static int xsslkey(XrdOucStream &Config)
Definition: XrdSecInterface.hh:551
int len
Definition: XrdHttpProtocol.hh:396
void BuffConsume(int blen)
Consume some bytes from the buffer.
Definition: XrdHttpReq.hh:77
XrdHttpReq CurrentReq
Definition: XrdHttpProtocol.hh:343
static int xlistredir(XrdOucStream &Config)
int doStat(char *fname)
Perform a Stat request.
XrdHttpProtocol(const XrdHttpProtocol &)=default
Ctor, dtors and copy ctor.
static char * sslcipherfilter
Definition: XrdHttpProtocol.hh:363
static char * gridmap
Gridmap file location. The same used by XrdSecGsi.
Definition: XrdHttpProtocol.hh:366
int getDataOneShot(int blen, bool wait=false)
static int xstaticredir(XrdOucStream &Config)
Definition: XrdOucStream.hh:46
Definition: XrdHttpProtocol.hh:221
Definition: XrdXrootdBridge.hh:61
static int xhttpsmode(XrdOucStream &Config)
static bool listdeny
If true, any form of listing is denied.
Definition: XrdHttpProtocol.hh:382
XrdBuffer * myBuff
Circular Buffer used to read the request.
Definition: XrdHttpProtocol.hh:245
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:129
static int xstaticpreload(XrdOucStream &Config)
static bool InitTLS()
Initialization of the ssl security things.
XrdObject< XrdHttpProtocol > ProtLink
Definition: XrdHttpProtocol.hh:122
Definition: XrdProtocol.hh:124
static XrdHttpSecXtractor * secxtractor
Definition: XrdHttpProtocol.hh:213
char * myBuffStart
The circular pointers.
Definition: XrdHttpProtocol.hh:247
static bool ExtHandlerLoaded(const char *handlername)
Definition: XrdXrootdProtocol.hh:154
static bool isRequiredXtractor
Definition: XrdHttpProtocol.hh:212
static XrdOucHash< StaticPreloadInfo > * staticpreload
Definition: XrdHttpProtocol.hh:398
static const char * Configed()
static bool isRequiredGridmap
Definition: XrdHttpProtocol.hh:367
Definition: XrdBuffer.hh:71
static BIO * sslbio_err
bio to print SSL errors
Definition: XrdHttpProtocol.hh:293
static XrdSecService * CIA
Definition: XrdHttpProtocol.hh:325
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:395
XrdHttpExtHandler * ptr
Definition: XrdHttpProtocol.hh:223
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:290
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:357
static int sslverifydepth
Depth of verification of a certificate chain.
Definition: XrdHttpProtocol.hh:373
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 XrdScheduler * Sched
Definition: XrdHttpProtocol.hh:322
Definition: XrdHttpProtocol.hh:178
int HandleGridMap(XrdLink *lp)
static int xsslverifydepth(XrdOucStream &Config)
int GetVOMSData(XrdLink *lp)
~extHInfo()
Definition: XrdHttpProtocol.hh:185
char name[16]
Definition: XrdHttpProtocol.hh:222
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:338
static bool selfhttps2http
If client is HTTPS, self-redirect with HTTP+token.
Definition: XrdHttpProtocol.hh:385
XrdProtocol * Match(XrdLink *lp)
Tells if the oustanding bytes on the socket match this protocol implementation.
Definition: XrdOucGMap.hh:48
~XrdHttpProtocol()
Definition: XrdHttpProtocol.hh:117
static int xlistdeny(XrdOucStream &Config)
static XrdBuffManager * BPool
Definition: XrdHttpProtocol.hh:323
Definition: XrdProtocol.hh:55
Definition: XrdOucEnv.hh:41
#define MAX_XRDHTTPEXTHANDLERS
Definition: XrdHttpProtocol.hh:220
int HandleAuthentication(XrdLink *lp)
void DoIt()
Override from the base class.
Definition: XrdHttpProtocol.hh:87
static XrdOucGMap * servGMap
The instance of the DN mapper. Created only when a valid path is given.
Definition: XrdHttpProtocol.hh:335
XrdLink * Link
The link we are bound to.
Definition: XrdHttpProtocol.hh:328
Definition: XrdHttpExtHandler.hh:79
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:179
static int hailWait
Timeout for reading the handshake.
Definition: XrdHttpProtocol.hh:351
bool ishttps
Tells if the client is https.
Definition: XrdHttpProtocol.hh:296
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:404
static XrdSysError eDest
Definition: XrdHttpProtocol.hh:324
static int m_bio_type
Type identifier for our custom BIO objects.
Definition: XrdHttpProtocol.hh:407
Definition: XrdHttpProtocol.hh:76
static char * sslcert
OpenSSL stuff.
Definition: XrdHttpProtocol.hh:363
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 &lt; 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:250
static BIO_METHOD * m_bio_method
C-style vptr table for our custom BIO objects.
Definition: XrdHttpProtocol.hh:410
int StartChunkedResp(int code, const char *desc, const char *header_to_add, bool keepalive)
Starts a chunked response; body of request is sent over multiple parts using the SendChunkResp.
static XrdCryptoFactory * myCryptoFactory
Definition: XrdHttpProtocol.hh:301
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:284
Definition: XrdHttpSecXtractor.hh:41
SSL * ssl
Private SSL context.
Definition: XrdHttpProtocol.hh:287
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:379
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:391
int kXR_int32
Definition: XPtypes.hh:89
XrdOucString extHParm
Definition: XrdHttpProtocol.hh:181
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:388
Definition: XrdSecEntity.hh:63
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:363
bool DoingLogin
Tells that we are just logging in.
Definition: XrdHttpProtocol.hh:281
int Process(XrdLink *lp)
Process data incoming from the socket.
XrdOucString extHPath
Definition: XrdHttpProtocol.hh:180
Definition: XrdHttpExtHandler.hh:45
bool ssldone
Definition: XrdHttpProtocol.hh:300
static int xsslcafile(XrdOucStream &Config)
Definition: XrdHttpProtocol.hh:394
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:183
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)