XRootD
Loading...
Searching...
No Matches
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
43
44#ifndef ENODATA
45#define ENODATA ENOATTR
46#endif
47
48/******************************************************************************/
49/* G l o b a l s */
50/******************************************************************************/
51
52namespace XrdProxy
53{
54
56
57}
58
59using 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//
69extern "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 csTab[3].Len = 4; strcpy(csTab[3].Name, "crc32c");
89 csLast = 3;
90}
91
92/******************************************************************************/
93/* Private: F i n d */
94/******************************************************************************/
95
96XrdPssCks::csInfo *XrdPssCks::Find(const char *Name)
97{
98 int i;
99 for (i = 0; i <= csLast; i++)
100 if (!strcmp(Name, csTab[i].Name)) return &csTab[i];
101 return 0;
102}
103
104/******************************************************************************/
105/* G e t */
106/******************************************************************************/
107
108int XrdPssCks::Get(const char *Pfn, XrdCksData &Cks)
109{
110 EPNAME("GetCks");
111 static const int cksBLen = 256;
112 static const int urlBLen = 2048;
113 char cksBuff[cksBLen], pBuff[urlBLen], cgiBuff[32], *tP;
114 XrdOucTokenizer Resp(cksBuff);
115 time_t Mtime;
116 int rc, n;
117
118// Construct the cgi for digest type
119//
120 n = snprintf(cgiBuff, sizeof(cgiBuff), "cks.type=%s", Cks.Name);
121 if (n >= (int)sizeof(cgiBuff)) return -ENAMETOOLONG;
122
123// Construct the correct url info
124//
125 XrdPssUrlInfo uInfo(Cks.envP, Pfn, cgiBuff, true);
126 uInfo.setID();
127
128// Direct the path to the origin
129//
130 if ((rc = XrdPssSys::P2URL(pBuff, sizeof(pBuff), uInfo))) return rc;
131
132// Do some debugging
133//
134
135 if(DEBUGON) {
136 auto urlObf = obfuscateAuth(pBuff);
137 DEBUG(uInfo.Tident(),"url="<<urlObf);
138 }
139
140// First step is to getthe checksum value
141//
142 if ((rc = XrdPosixXrootd::QueryChksum(pBuff, Mtime, cksBuff, cksBLen)) <= 0)
143 return (rc ? -errno : -ENOTSUP);
144
145// Get the checksum name
146//
147 if (!Resp.GetLine() || !(tP = Resp.GetToken()) || !(*tP)) return -ENOMSG;
148 if (!Cks.Set(tP)) return -ENOTSUP;
149
150// Now get the checksum value
151//
152 if (!(tP = Resp.GetToken()) || !(*tP)) return -ENODATA;
153 if (!Cks.Set(tP, strlen(tP))) return -ENOTSUP;
154
155// Set remaining fields and return success
156//
157 Cks.fmTime = static_cast<long long>(Mtime);
158 Cks.csTime = 0;
159 return Cks.Length;
160}
161
162/******************************************************************************/
163/* I n i t */
164/******************************************************************************/
165
166int XrdPssCks::Init(const char *ConfigFN, const char *DfltCalc)
167{
168 int i;
169
170// See if we need to set the default calculation
171//
172 if (DfltCalc)
173 {for (i = 0; i <= csLast; i++) if (!strcmp(csTab[i].Name, DfltCalc)) break;
174 if (i >= csMax)
175 {eDest->Emsg("Config", DfltCalc, "cannot be made the default; "
176 "not supported.");
177 return 0;
178 }
179 if (i) {csInfo Temp = csTab[i]; csTab[i] = csTab[0]; csTab[0] = Temp;}
180 }
181
182// All done
183//
184 return 1;
185}
186
187/******************************************************************************/
188/* N a m e */
189/******************************************************************************/
190
191const char *XrdPssCks::Name(int seqNum)
192{
193
194 return (seqNum < 0 || seqNum > csLast ? 0 : csTab[seqNum].Name);
195}
196
197/******************************************************************************/
198/* S i z e */
199/******************************************************************************/
200
201int XrdPssCks::Size(const char *Name)
202{
203 csInfo *iP = (Name != 0 ? Find(Name) : &csTab[0]);
204 return (iP != 0 ? iP->Len : 0);
205}
206
207/******************************************************************************/
208/* V e r */
209/******************************************************************************/
210
211int XrdPssCks::Ver(const char *Pfn, XrdCksData &Cks)
212{
213 XrdCksData fCks;
214 csInfo *csIP = &csTab[0];
215 int rc;
216
217// Determine which checksum to get
218//
219 if (!(*Cks.Name)) strcpy(Cks.Name, csTab[0].Name);
220 else if (!(csIP = Find(Cks.Name))) return -ENOTSUP;
221
222// Get the file checksum
223//
224 if ((rc = Get(Pfn, fCks))) return rc;
225
226// Compare the checksums
227//
228 return (!strcmp(fCks.Name, Cks.Name) && fCks.Length == Cks.Length
229 && !memcmp(fCks.Value, Cks.Value, csIP->Len));
230}
#define DEBUG(x)
#define EPNAME(x)
#define ENODATA
std::string obfuscateAuth(const std::string &input)
#define DEBUGON
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 Name[NameSize]
Definition XrdCksData.hh:44
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:108
virtual int Ver(const char *Pfn, XrdCksData &Cks)
Definition XrdPssCks.cc:211
virtual int Init(const char *ConfigFN, const char *DfltCalc=0)
Definition XrdPssCks.cc:166
virtual const char * Name(int seqNum=0)
Definition XrdPssCks.cc:191
XrdPssCks(XrdSysError *erP)
Definition XrdPssCks.cc:80
virtual int Size(const char *Name=0)
Definition XrdPssCks.cc:201
static int P2URL(char *pbuff, int pblen, XrdPssUrlInfo &uInfo, bool doN2N=true)
Definition XrdPss.cc:1417
const char * Tident()
void setID(const char *tid=0)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
XrdSysTrace SysTrace("Pss", 0)
Definition XrdPssCks.cc:55
XrdSysError eDest(0, "pss_")