XRootD
XrdPssCks.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d P s s C k s . c c */
4 /* */
5 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <cstdio>
32 
33 #include "XrdPss/XrdPss.hh"
34 #include "XrdPss/XrdPssCks.hh"
35 #include "XrdPss/XrdPssTrace.hh"
36 
37 #include "XrdVersion.hh"
38 
42 #include "XrdPss/XrdPssUrlInfo.hh"
43 
44 #ifndef ENODATA
45 #define ENODATA ENOATTR
46 #endif
47 
48 /******************************************************************************/
49 /* G l o b a l s */
50 /******************************************************************************/
51 
52 namespace XrdProxy
53 {
54 
56 
57 }
58 
59 using namespace XrdProxy;
60 
61 /******************************************************************************/
62 /* X r d C k s I n i t */
63 /******************************************************************************/
64 
66 
67 // Return the proxy checksum object created by the storage system interface.
68 //
69 extern "C"
70 {XrdCks *XrdCksInit(XrdSysError *eDest, // The error msg object
71  const char *cFN, // Config file name
72  const char *Parms // Parms via lib directive
73  ) {return (XrdCks *)new XrdPssCks(eDest);}
74 }
75 
76 /******************************************************************************/
77 /* C o n s t r u c t o r */
78 /******************************************************************************/
79 
81 {
82 
83 // Prefill the native digests we support
84 //
85  csTab[0].Len = 4; strcpy(csTab[0].Name, "adler32");
86  csTab[1].Len = 4; strcpy(csTab[1].Name, "crc32");
87  csTab[2].Len = 16; strcpy(csTab[2].Name, "md5");
88  csLast = 2;
89 }
90 
91 /******************************************************************************/
92 /* Private: F i n d */
93 /******************************************************************************/
94 
95 XrdPssCks::csInfo *XrdPssCks::Find(const char *Name)
96 {
97  int i;
98  for (i = 0; i <= csLast; i++)
99  if (!strcmp(Name, csTab[i].Name)) return &csTab[i];
100  return 0;
101 }
102 
103 /******************************************************************************/
104 /* G e t */
105 /******************************************************************************/
106 
107 int XrdPssCks::Get(const char *Pfn, XrdCksData &Cks)
108 {
109  EPNAME("GetCks");
110  static const int cksBLen = 256;
111  static const int urlBLen = 2048;
112  char cksBuff[cksBLen], pBuff[urlBLen], cgiBuff[32], *tP;
113  XrdOucTokenizer Resp(cksBuff);
114  time_t Mtime;
115  int rc, n;
116 
117 // Construct the cgi for digest type
118 //
119  n = snprintf(cgiBuff, sizeof(cgiBuff), "cks.type=%s", Cks.Name);
120  if (n >= (int)sizeof(cgiBuff)) return -ENAMETOOLONG;
121 
122 // Construct the correct url info
123 //
124  XrdPssUrlInfo uInfo(Cks.envP, Pfn, cgiBuff, false);
125  uInfo.setID();
126 
127 // Direct the path to the origin
128 //
129  if ((rc = XrdPssSys::P2URL(pBuff, sizeof(pBuff), uInfo))) return rc;
130 
131 // Do some debugging
132 //
133 
134  if(DEBUGON) {
135  auto urlObf = obfuscateAuth(pBuff);
136  DEBUG(uInfo.Tident(),"url="<<urlObf);
137  }
138 
139 // First step is to getthe checksum value
140 //
141  if ((rc = XrdPosixXrootd::QueryChksum(pBuff, Mtime, cksBuff, cksBLen)) <= 0)
142  return (rc ? -errno : -ENOTSUP);
143 
144 // Get the checksum name
145 //
146  if (!Resp.GetLine() || !(tP = Resp.GetToken()) || !(*tP)) return -ENOMSG;
147  if (!Cks.Set(tP)) return -ENOTSUP;
148 
149 // Now get the checksum value
150 //
151  if (!(tP = Resp.GetToken()) || !(*tP)) return -ENODATA;
152  if (!Cks.Set(tP, strlen(tP))) return -ENOTSUP;
153 
154 // Set remaining fields and return success
155 //
156  Cks.fmTime = static_cast<long long>(Mtime);
157  Cks.csTime = 0;
158  return Cks.Length;
159 }
160 
161 /******************************************************************************/
162 /* I n i t */
163 /******************************************************************************/
164 
165 int XrdPssCks::Init(const char *ConfigFN, const char *DfltCalc)
166 {
167  int i;
168 
169 // See if we need to set the default calculation
170 //
171  if (DfltCalc)
172  {for (i = 0; i < csLast; i++) if (!strcmp(csTab[i].Name, DfltCalc)) break;
173  if (i >= csMax)
174  {eDest->Emsg("Config", DfltCalc, "cannot be made the default; "
175  "not supported.");
176  return 0;
177  }
178  if (i) {csInfo Temp = csTab[i]; csTab[i] = csTab[0]; csTab[0] = Temp;}
179  }
180 
181 // All done
182 //
183  return 1;
184 }
185 
186 /******************************************************************************/
187 /* N a m e */
188 /******************************************************************************/
189 
190 const char *XrdPssCks::Name(int seqNum)
191 {
192 
193  return (seqNum < 0 || seqNum > csLast ? 0 : csTab[seqNum].Name);
194 }
195 
196 /******************************************************************************/
197 /* S i z e */
198 /******************************************************************************/
199 
200 int XrdPssCks::Size(const char *Name)
201 {
202  csInfo *iP = (Name != 0 ? Find(Name) : &csTab[0]);
203  return (iP != 0 ? iP->Len : 0);
204 }
205 
206 /******************************************************************************/
207 /* V e r */
208 /******************************************************************************/
209 
210 int XrdPssCks::Ver(const char *Pfn, XrdCksData &Cks)
211 {
212  XrdCksData fCks;
213  csInfo *csIP = &csTab[0];
214  int rc;
215 
216 // Determine which checksum to get
217 //
218  if (!(*Cks.Name)) strcpy(Cks.Name, csTab[0].Name);
219  else if (!(csIP = Find(Cks.Name))) return -ENOTSUP;
220 
221 // Get the file checksum
222 //
223  if ((rc = Get(Pfn, fCks))) return rc;
224 
225 // Compare the checksums
226 //
227  return (!strcmp(fCks.Name, Cks.Name) && fCks.Length == Cks.Length
228  && !memcmp(fCks.Value, Cks.Value, csIP->Len));
229 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
std::string obfuscateAuth(const std::string &input)
#define DEBUGON
#define ENODATA
Definition: XrdPssCks.cc:45
XrdCks * XrdCksInit(XrdSysError *eDest, const char *cFN, const char *Parms)
Definition: XrdPssCks.cc:70
XrdVERSIONINFO(XrdCksInit, PssCks)
char Value[ValuSize]
Definition: XrdCksData.hh:53
int Set(const char *csName)
Definition: XrdCksData.hh:81
char Length
Definition: XrdCksData.hh:52
char Name[NameSize]
Definition: XrdCksData.hh:44
Definition: XrdCks.hh:92
XrdSysError * eDest
Definition: XrdCks.hh:289
char * GetToken(char **rest=0, int lowcase=0)
static int QueryChksum(const char *path, time_t &mtime, char *buff, int blen)
virtual int Get(const char *Pfn, XrdCksData &Cks)
Definition: XrdPssCks.cc:107
virtual int Ver(const char *Pfn, XrdCksData &Cks)
Definition: XrdPssCks.cc:210
virtual int Init(const char *ConfigFN, const char *DfltCalc=0)
Definition: XrdPssCks.cc:165
virtual const char * Name(int seqNum=0)
Definition: XrdPssCks.cc:190
XrdPssCks(XrdSysError *erP)
Definition: XrdPssCks.cc:80
virtual int Size(const char *Name=0)
Definition: XrdPssCks.cc:200
static int P2URL(char *pbuff, int pblen, XrdPssUrlInfo &uInfo, bool doN2N=true)
Definition: XrdPss.cc:1402
const char * Tident()
void setID(const char *tid=0)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
XrdSysTrace SysTrace("Pss", 0)
Definition: XrdPssCks.cc:55
XrdSysError eDest(0, "pss_")