XRootD
XrdOucUtils.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c U t i l s . c c */
4 /* */
5 /* (c) 2005 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 <cctype>
32 #include <grp.h>
33 #include <cstdio>
34 #include <list>
35 #include <vector>
36 #include <unordered_set>
37 #include <algorithm>
38 
39 #include <regex.h>
40 
41 #ifdef WIN32
42 #include <direct.h>
43 #include "XrdSys/XrdWin32.hh"
44 #else
45 #include <fcntl.h>
46 #include <math.h>
47 #include <pwd.h>
48 #include <sys/stat.h>
49 #include <sys/types.h>
50 #endif
51 #include <map>
52 #include "XrdNet/XrdNetUtils.hh"
53 #include "XrdOuc/XrdOucCRC.hh"
54 #include "XrdOuc/XrdOucEnv.hh"
55 #include "XrdOuc/XrdOucSHA3.hh"
56 #include "XrdOuc/XrdOucStream.hh"
57 #include "XrdOuc/XrdOucString.hh"
58 #include "XrdOuc/XrdOucUtils.hh"
60 #include "XrdSys/XrdSysE2T.hh"
61 #include "XrdSys/XrdSysError.hh"
62 #include "XrdSys/XrdSysPlatform.hh"
63 #include "XrdSys/XrdSysPthread.hh"
64 
65 #ifndef ENODATA
66 #define ENODATA ENOATTR
67 #endif
68 
69 /******************************************************************************/
70 /* L o c a l M e t h o d s */
71 /******************************************************************************/
72 
73 namespace
74 {
75 struct idInfo
76 { time_t Expr;
77  char *Name;
78 
79  idInfo(const char *name, time_t keep)
80  : Expr(time(0)+keep), Name(strdup(name)) {}
81  ~idInfo() {free(Name);}
82 };
83 
84 typedef std::map<unsigned int, struct idInfo*> idMap_t;
85 
86 idMap_t gidMap;
87 idMap_t uidMap;
88 XrdSysMutex idMutex;
89 
90 void AddID(idMap_t &idMap, unsigned int id, const char *name, time_t keepT)
91 {
92  std::pair<idMap_t::iterator,bool> ret;
93  idInfo *infoP = new idInfo(name, keepT);
94 
95  idMutex.Lock();
96  ret = idMap.insert(std::pair<unsigned int, struct idInfo*>(id, infoP));
97  if (ret.second == false) delete infoP;
98  idMutex.UnLock();
99 }
100 
101 int LookUp(idMap_t &idMap, unsigned int id, char *buff, int blen)
102 {
103  idMap_t::iterator it;
104  int luRet = 0;
105 
106  idMutex.Lock();
107  it = idMap.find(id);
108  if (it != idMap.end())
109  {if (it->second->Expr <= time(0))
110  {delete it->second;
111  idMap.erase(it);
112  } else {
113  if (blen > 0) luRet = snprintf(buff, blen, "%s", it->second->Name);
114  }
115  }
116  idMutex.UnLock();
117  return luRet;
118 }
119 }
120 
121 /******************************************************************************/
122 /* a r g L i s t */
123 /******************************************************************************/
124 
125 int XrdOucUtils::argList(char *args, char **argV, int argC)
126 {
127  char *aP = args;
128  int j;
129 
130 // Construct the argv array based on passed command line.
131 //
132 for (j = 0; j < argC; j++)
133  {while(*aP == ' ') aP++;
134  if (!(*aP)) break;
135 
136  if (*aP == '"' || *aP == '\'')
137  {argV[j] = aP+1;
138  aP = index(aP+1, *aP);
139  if (!aP || (*(aP+1) != ' ' && *(aP+1)))
140  {if (!j) argV[0] = 0; return -EINVAL;}
141  *aP++ = '\0';
142  } else {
143  argV[j] = aP;
144  if ((aP = index(aP+1, ' '))) *aP++ = '\0';
145  else {j++; break;}
146  }
147 
148  }
149 
150 // Make sure we did not overflow the vector
151 //
152  if (j > argC-1) return -E2BIG;
153 
154 // End list with a null pointer and return the actual number of arguments
155 //
156  argV[j] = 0;
157  return j;
158 }
159 
160 /******************************************************************************/
161 /* b i n 2 h e x */
162 /******************************************************************************/
163 
164 char *XrdOucUtils::bin2hex(char *inbuff, int dlen, char *buff, int blen,
165  bool sep)
166 {
167  static char hv[] = "0123456789abcdef";
168  char *outbuff = buff;
169  for (int i = 0; i < dlen && blen > 2; i++) {
170  *outbuff++ = hv[(inbuff[i] >> 4) & 0x0f];
171  *outbuff++ = hv[ inbuff[i] & 0x0f];
172  blen -= 2;
173  if (sep && blen > 1 && ((i & 0x03) == 0x03 || i+1 == dlen))
174  {*outbuff++ = ' '; blen--;}
175  }
176  *outbuff = '\0';
177  return buff;
178 }
179 
180 /******************************************************************************/
181 /* e n d s W i t h */
182 /******************************************************************************/
183 
184 bool XrdOucUtils::endsWith(const char *text, const char *ending, int endlen)
185 {
186  int tlen = strlen(text);
187 
188  return (tlen >= endlen && !strcmp(text+(tlen-endlen), ending));
189 }
190 
191 /******************************************************************************/
192 /* e T e x t */
193 /******************************************************************************/
194 
195 // eText() returns the text associated with the error.
196 // The text buffer pointer is returned.
197 
198 char *XrdOucUtils::eText(int rc, char *eBuff, int eBlen)
199 {
200  const char *etP;
201 
202 // Get error text
203 //
204  etP = XrdSysE2T(rc);
205 
206 // Copy the text and lower case the first letter
207 //
208  strlcpy(eBuff, etP, eBlen);
209 
210 // All done
211 //
212  return eBuff;
213 }
214 
215 /******************************************************************************/
216 /* d o I f */
217 /******************************************************************************/
218 
219 // doIf() parses "if [<hostlist>] [<conds>]"
220 // conds: <cond1> | <cond2> | <cond3>
221 // cond1: defined <varlist> [&& <conds>]
222 // cond2: exec <pgmlist> [&& <cond3>]
223 // cond3: named <namelist>
224 
225 // Returning 1 if true (i.e., this machine is one of the named hosts in hostlist
226 // and is running one of the programs pgmlist and named by one of the names in
227 // namelist).
228 // Return -1 (negative truth) if an error occurred.
229 // Otherwise, returns false (0). Some combination of hostlist, pgm, and
230 // namelist, must be specified.
231 
233  const char *what, const char *hname,
234  const char *nname, const char *pname)
235 {
236  static const char *brk[] = {"defined", "exec", "named", 0};
237  XrdOucEnv *theEnv = 0;
238  char *val;
239  int hostok, isDef;
240 
241 // Make sure that at least one thing appears after the if
242 //
243  if (!(val = Config.GetWord()))
244  {if (eDest) eDest->Emsg("Config","Host name missing after 'if' in", what);
245  return -1;
246  }
247 
248 // Check if we are one of the listed hosts
249 //
250  if (!is1of(val, brk))
251  {do {hostok = XrdNetUtils::Match(hname, val);
252  val = Config.GetWord();
253  } while(!hostok && val && !is1of(val, brk));
254  if (hostok)
255  { while(val && !is1of(val, brk)) val = Config.GetWord();
256  // No more directives
257  if (!val) return 1;
258  } else return 0;
259  }
260 
261 // Check if this is a defined test
262 //
263  while(!strcmp(val, "defined"))
264  {if (!(val = Config.GetWord()) || *val != '?')
265  {if (eDest)
266  {eDest->Emsg("Config","'?var' missing after 'defined' in",what);}
267  return -1;
268  }
269  // Get environment if we have none
270  //
271  if (!theEnv && (theEnv = Config.SetEnv(0))) Config.SetEnv(theEnv);
272  if (!theEnv && *(val+1) != '~') return 0;
273 
274  // Check if any listed variable is defined.
275  //
276  isDef = 0;
277  while(val && *val == '?')
278  {if (*(val+1) == '~' ? getenv(val+2) : theEnv->Get(val+1)) isDef=1;
279  val = Config.GetWord();
280  }
281  if (!val || !isDef) return isDef;
282  if (strcmp(val, "&&"))
283  {if (eDest)
284  {eDest->Emsg("Config",val,"is invalid for defined test in",what);}
285  return -1;
286  } else {
287  if (!(val = Config.GetWord()))
288  {if (eDest)
289  {eDest->Emsg("Config","missing keyword after '&&' in",what);}
290  return -1;
291  }
292  }
293  if (!is1of(val, brk))
294  {if (eDest)
295  {eDest->Emsg("Config",val,"is invalid after '&&' in",what);}
296  return -1;
297  }
298  }
299 
300 // Check if we need to compare program names (we are here only if we either
301 // passed the hostlist test or there was no hostlist present)
302 //
303  if (!strcmp(val, "exec"))
304  {if (!(val = Config.GetWord()) || !strcmp(val, "&&"))
305  {if (eDest)
306  {eDest->Emsg("Config","Program name missing after 'if exec' in",what);}
307  return -1;
308  }
309 
310  // Check if we are one of the programs.
311  //
312  if (!pname) return 0;
313  while(val && strcmp(val, pname))
314  if (!strcmp(val, "&&")) return 0;
315  else val = Config.GetWord();
316  if (!val) return 0;
317  while(val && strcmp(val, "&&")) val = Config.GetWord();
318  if (!val) return 1;
319 
320  if (!(val = Config.GetWord()))
321  {if (eDest)
322  {eDest->Emsg("Config","Keyword missing after '&&' in",what);}
323  return -1;
324  }
325  if (strcmp(val, "named"))
326  {if (eDest)
327  {eDest->Emsg("Config",val,"is invalid after '&&' in",what);}
328  return -1;
329  }
330  }
331 
332 // Check if we need to compare net names (we are here only if we either
333 // passed the hostlist test or there was no hostlist present)
334 //
335  if (!(val = Config.GetWord()))
336  {if (eDest)
337  {eDest->Emsg("Config","Instance name missing after 'if named' in", what);}
338  return -1;
339  }
340 
341 // Check if we are one of the names
342 //
343  if (!nname) return 0;
344  while(val && strcmp(val, nname)) val = Config.GetWord();
345 
346 // All done
347 //
348  return (val != 0);
349 }
350 
351 /******************************************************************************/
352 /* f i n d P g m */
353 /******************************************************************************/
354 
355 bool XrdOucUtils::findPgm(const char *pgm, XrdOucString& path)
356 {
357  struct stat Stat;
358 
359 // Check if only executable bit needs to be checked
360 //
361  if (*pgm == '/')
362  {if (stat(pgm, &Stat) || !(Stat.st_mode & S_IXOTH)) return false;
363  path = pgm;
364  return true;
365  }
366 
367 // Make sure we have the paths to check
368 //
369  const char *pEnv = getenv("PATH");
370  if (!pEnv) return false;
371 
372 // Setup to find th executable
373 //
374  XrdOucString prog, pList(pEnv);
375  int from = 0;;
376  prog += '/'; prog += pgm;
377 
378 // Find it!
379 //
380  while((from = pList.tokenize(path, from, ':')) != -1)
381  {path += prog;
382  if (!stat(path.c_str(), &Stat) && Stat.st_mode & S_IXOTH) return true;
383  }
384  return false;
385 }
386 
387 /******************************************************************************/
388 /* f m t B y t e s */
389 /******************************************************************************/
390 
391 int XrdOucUtils::fmtBytes(long long val, char *buff, int bsz)
392 {
393  static const long long Kval = 1024LL;
394  static const long long Mval = 1024LL*1024LL;
395  static const long long Gval = 1024LL*1024LL*1024LL;
396  static const long long Tval = 1024LL*1024LL*1024LL*1024LL;
397  char sName = ' ';
398  int resid;
399 
400 // Get correct scaling
401 //
402  if (val < 1024) return snprintf(buff, bsz, "%lld", val);
403  if (val < Mval) {val = val*10/Kval; sName = 'K';}
404  else if (val < Gval) {val = val*10/Mval; sName = 'M';}
405  else if (val < Tval) {val = val*10/Gval; sName = 'G';}
406  else {val = val*10/Tval; sName = 'T';}
407  resid = val%10LL; val = val/10LL;
408 
409 // Format it
410 //
411  return snprintf(buff, bsz, "%lld.%d%c", val, resid, sName);
412 }
413 
414 /******************************************************************************/
415 /* g e n P a t h */
416 /******************************************************************************/
417 
418 char *XrdOucUtils::genPath(const char *p_path, const char *inst,
419  const char *s_path)
420 {
421  char buff[2048];
422  int i = strlcpy(buff, p_path, sizeof(buff));
423 
424  if (buff[i-1] != '/') {buff[i++] = '/'; buff[i] = '\0';}
425  if (inst) {strcpy(buff+i, inst); strcat(buff, "/");}
426  if (s_path) strcat(buff, s_path);
427 
428  i = strlen(buff);
429  if (buff[i-1] != '/') {buff[i++] = '/'; buff[i] = '\0';}
430 
431  return strdup(buff);
432 }
433 
434 /******************************************************************************/
435 
436 int XrdOucUtils::genPath(char *buff, int blen, const char *path, const char *psfx)
437 {
438  int i, j;
439 
440  i = strlen(path);
441  j = (psfx ? strlen(psfx) : 0);
442  if (i+j+3 > blen) return -ENAMETOOLONG;
443 
444  strcpy(buff, path);
445  if (psfx)
446  {if (buff[i-1] != '/') buff[i++] = '/';
447  strcpy(&buff[i], psfx);
448  if (psfx[j-1] != '/') strcat(buff, "/");
449  }
450  return 0;
451 }
452 
453 /******************************************************************************/
454 /* g e t F i l e */
455 /******************************************************************************/
456 
457 char *XrdOucUtils::getFile(const char *path, int &rc, int maxsz, bool notempty)
458 {
459  struct stat Stat;
460  struct fdHelper
461  {int fd = -1;
462  fdHelper() {}
463  ~fdHelper() {if (fd >= 0) close(fd);}
464  } file;
465  char *buff;
466  int flen;
467 
468 // Preset RC
469 //
470  rc = 0;
471 
472 // Open the file in read mode
473 //
474  if ((file.fd = open(path, O_RDONLY)) < 0) {rc = errno; return 0;}
475 
476 // Get the size of the file
477 //
478  if (fstat(file.fd, &Stat)) {rc = errno; return 0;}
479 
480 // Check if the size exceeds the maximum allowed
481 //
482  if (Stat.st_size > maxsz) {rc = EFBIG; return 0;}
483 
484 // Make sure the file is not empty if empty files are disallowed
485 //
486  if (Stat.st_size == 0 && notempty) {rc = ENODATA; return 0;}
487 
488 // Allocate a buffer
489 //
490  if ((buff = (char *)malloc(Stat.st_size+1)) == 0)
491  {rc = errno; return 0;}
492 
493 // Read the contents of the file into the buffer
494 //
495  if (Stat.st_size)
496  {if ((flen = read(file.fd, buff, Stat.st_size)) < 0)
497  {rc = errno; free(buff); return 0;}
498  } else flen = 0;
499 
500 // Add null byte. recall the buffer is bigger by one byte
501 //
502  buff[flen] = 0;
503 
504 // Return the size aand the buffer
505 //
506  rc = flen;
507  return buff;
508 }
509 
510 /******************************************************************************/
511 /* g e t G I D */
512 /******************************************************************************/
513 
514 bool XrdOucUtils::getGID(const char *gName, gid_t &gID)
515 {
516  struct group Grp, *result;
517  char buff[65536];
518 
519  getgrnam_r(gName, &Grp, buff, sizeof(buff), &result);
520  if (!result) return false;
521 
522  gID = Grp.gr_gid;
523  return true;
524 }
525 
526 /******************************************************************************/
527 /* g e t U I D */
528 /******************************************************************************/
529 
530 bool XrdOucUtils::getUID(const char *uName, uid_t &uID, gid_t *gID)
531 {
532  struct passwd pwd, *result;
533  char buff[16384];
534 
535  getpwnam_r(uName, &pwd, buff, sizeof(buff), &result);
536  if (!result) return false;
537 
538  uID = pwd.pw_uid;
539  if (gID) *gID = pwd.pw_gid;
540 
541  return true;
542 }
543 
544 /******************************************************************************/
545 /* G i d N a m e */
546 /******************************************************************************/
547 
548 int XrdOucUtils::GidName(gid_t gID, char *gName, int gNsz, time_t keepT)
549 {
550  static const int maxgBsz = 256*1024;
551  static const int addGsz = 4096;
552  struct group *gEnt, gStruct;
553  char gBuff[1024], *gBp = gBuff;
554  int glen = 0, gBsz = sizeof(gBuff), aOK = 1;
555  int n, retVal = 0;
556 
557 // Get ID from cache, if allowed
558 //
559  if (keepT)
560  {int n = LookUp(gidMap, static_cast<unsigned int>(gID),gName,gNsz);
561  if (n > 0) return (n < gNsz ? n : 0);
562  }
563 
564 // Get the the group struct. If we don't have a large enough buffer, get a
565 // larger one and try again up to the maximum buffer we will tolerate.
566 //
567  while(( retVal = getgrgid_r(gID, &gStruct, gBp, gBsz, &gEnt) ) == ERANGE)
568  {if (gBsz >= maxgBsz) {aOK = 0; break;}
569  if (gBsz > addGsz) free(gBp);
570  gBsz += addGsz;
571  if (!(gBp = (char *)malloc(gBsz))) {aOK = 0; break;}
572  }
573 
574 // Return a group name if all went well
575 //
576  if (aOK && retVal == 0 && gEnt != NULL)
577  {if (keepT)
578  AddID(gidMap, static_cast<unsigned int>(gID), gEnt->gr_name, keepT);
579  glen = strlen(gEnt->gr_name);
580  if (glen >= gNsz) glen = 0;
581  else strcpy(gName, gEnt->gr_name);
582  } else {
583  n = snprintf(gName, gNsz, "%ud", static_cast<unsigned int>(gID));
584  if (n >= gNsz) glen = 0;
585  }
586 
587 // Free any allocated buffer and return result
588 //
589  if (gBsz > addGsz && gBp) free(gBp);
590  return glen;
591 }
592 
593 /******************************************************************************/
594 /* G r o u p N a m e */
595 /******************************************************************************/
596 
597 int XrdOucUtils::GroupName(gid_t gID, char *gName, int gNsz)
598 {
599  static const int maxgBsz = 256*1024;
600  static const int addGsz = 4096;
601  struct group *gEnt, gStruct;
602  char gBuff[1024], *gBp = gBuff;
603  int glen, gBsz = sizeof(gBuff), aOK = 1;
604  int retVal = 0;
605 
606 // Get the the group struct. If we don't have a large enough buffer, get a
607 // larger one and try again up to the maximum buffer we will tolerate.
608 //
609  while(( retVal = getgrgid_r(gID, &gStruct, gBp, gBsz, &gEnt) ) == ERANGE)
610  {if (gBsz >= maxgBsz) {aOK = 0; break;}
611  if (gBsz > addGsz) free(gBp);
612  gBsz += addGsz;
613  if (!(gBp = (char *)malloc(gBsz))) {aOK = 0; break;}
614  }
615 
616 // Return a group name if all went well
617 //
618  if (aOK && retVal == 0 && gEnt != NULL)
619  {glen = strlen(gEnt->gr_name);
620  if (glen >= gNsz) glen = 0;
621  else strcpy(gName, gEnt->gr_name);
622  } else glen = 0;
623 
624 // Free any allocated buffer and return result
625 //
626  if (gBsz > addGsz && gBp) free(gBp);
627  return glen;
628 }
629 
630 /******************************************************************************/
631 /* H S i z e */
632 /******************************************************************************/
633 
634 const char *XrdOucUtils::HSize(size_t bytes, char* buff, int bsz)
635 {
636 
637 // Do fast conversion of the quantity is less than 1K
638 //
639  if (bytes < 1024)
640  {snprintf(buff, bsz, "%zu", bytes);
641  return buff;
642  }
643 
644 // Scale this down
645 //
646  const char *suffix = " KMGTPEYZ";
647  double dBytes = static_cast<double>(bytes);
648 
649 do{dBytes /= 1024.0; suffix++;
650  } while(dBytes >= 1024.0 && *(suffix+1));
651 
652 
653 // Format and return result. Include fractions only if they meaningfully exist.
654 //
655  double whole, frac = modf(dBytes, &whole);
656  if (frac >= .005) snprintf(buff, bsz, "%.02lf%c", dBytes, *suffix);
657  else snprintf(buff, bsz, "%g%c", whole, *suffix);
658  return buff;
659 }
660 
661 /******************************************************************************/
662 /* i 2 b s t r */
663 /******************************************************************************/
664 
665 const char *XrdOucUtils::i2bstr(char *buff, int blen, int val, bool pad)
666 {
667  char zo[2] = {'0', '1'};
668 
669  if (blen < 2) return "";
670 
671  buff[--blen] = 0;
672  if (!val) buff[blen--] = '0';
673  else while(val && blen >= 0)
674  {buff[blen--] = zo[val & 0x01];
675  val >>= 1;
676  }
677 
678  if (blen >= 0 && pad) while(blen >= 0) buff[blen--] = '0';
679 
680  return &buff[blen+1];
681 }
682 
683 /******************************************************************************/
684 /* I d e n t */
685 /******************************************************************************/
686 
687 namespace
688 {
689 long long genSID(char *&urSID, const char *iHost, int iPort,
690  const char *iName, const char *iProg)
691 {
692  static const XrdOucSHA3::MDLen mdLen = XrdOucSHA3::SHA3_512;
693  static const uint32_t fpOffs = 2, fpSize = 6; // 48 bit finger print
694 
695  const char *iSite = getenv("XRDSITE");
696  unsigned char mDigest[mdLen];
697  XrdOucString myID;
698  union {uint64_t mdLL; unsigned char mdUC[8];}; // Works for fpSize only!
699 
700 // Construct our unique identification
701 //
702  if (iSite) myID = iSite;
703  myID += iHost;
704  myID += iPort;
705  if (iName) myID += iName;
706  myID += iProg;
707 
708 // Generate a SHA3 digest of this string.
709 //
710  memset(mDigest, 0, sizeof(mDigest));
711  XrdOucSHA3::Calc(myID.c_str(), myID.length(), mDigest, mdLen);
712 
713 // Generate a CRC32C of the same string
714 //
715  uint32_t crc32c = XrdOucCRC::Calc32C(myID.c_str(), myID.length());
716 
717 // We need a 48-bit fingerprint that has a very low probability of collision.
718 // We accomplish this by convoluting the CRC32C checksum with the SHA3 checksum.
719 //
720  uint64_t fpPos = crc32c % (((uint32_t)mdLen) - fpSize);
721  mdLL = 0;
722  memcpy(mdUC+fpOffs, mDigest+fpPos, fpSize);
723  long long fpVal = static_cast<long long>(ntohll(mdLL));
724 
725 // Generate the character version of our fingerprint and return the binary one.
726 //
727  char fpBuff[64];
728  snprintf(fpBuff, sizeof(fpBuff), "%lld", fpVal);
729  urSID = strdup(fpBuff);
730  return fpVal;
731 }
732 }
733 
734 char *XrdOucUtils::Ident(long long &mySID, char *iBuff, int iBlen,
735  const char *iHost, const char *iProg,
736  const char *iName, int iPort)
737 {
738  static char *theSIN;
739  static long long theSID = genSID(theSIN, iHost, iPort, iName, iProg);
740  const char *sP = getenv("XRDSITE");
741  char uName[256];
742  int myPid = static_cast<int>(getpid());
743 
744 // Get our username
745 //
746  if (UserName(getuid(), uName, sizeof(uName)))
747  sprintf(uName, "%d", static_cast<int>(getuid()));
748 
749 // Create identification record
750 //
751  snprintf(iBuff,iBlen,"%s.%d:%s@%s\n&site=%s&port=%d&inst=%s&pgm=%s",
752  uName, myPid, theSIN, iHost, (sP ? sP : ""), iPort, iName, iProg);
753 
754 // Return a copy of the sid key
755 //
756  h2nll(theSID, mySID);
757  return strdup(theSIN);
758 }
759 
760 /******************************************************************************/
761 /* I n s t N a m e */
762 /******************************************************************************/
763 
764 const char *XrdOucUtils::InstName(int TranOpt)
765 {
766  const char *iName = getenv("XRDNAME");
767 
768 // If tran is zero, return what we have
769 //
770  if (!TranOpt) return iName;
771 
772 // If trans is positive then make sure iName has a value. Otherwise, make sure
773 // iName has no value if it's actually "anon".
774 //
775  if (TranOpt > 0) {if (!iName || !*iName) iName = "anon";}
776  else if (iName && !strcmp(iName, "anon")) iName = 0;
777  return iName;
778 }
779 /******************************************************************************/
780 
781 const char *XrdOucUtils::InstName(const char *name, int Fillit)
782 { return (Fillit ? name && *name ? name : "anon"
783  : name && strcmp(name,"anon") && *name ? name : 0);
784 }
785 
786 /******************************************************************************/
787 /* i s 1 o f */
788 /******************************************************************************/
789 
790 int XrdOucUtils::is1of(char *val, const char **clist)
791 {
792  int i = 0;
793  while(clist[i]) if (!strcmp(val, clist[i])) return 1;
794  else i++;
795  return 0;
796 }
797 
798 /******************************************************************************/
799 /* i s F W D */
800 /******************************************************************************/
801 
802 int XrdOucUtils::isFWD(const char *path, int *port, char *hBuff, int hBLen,
803  bool pTrim)
804 {
805  const char *hName, *hNend, *hPort, *hPend, *hP = path;
806  char *eP;
807  int n;
808 
809  if (*path == '/') hP++; // Note: It's assumed an objectid if no slash
810  if (*hP == 'x') hP++;
811  if (strncmp("root:/", hP, 6)) return 0;
812  if (hBuff == 0 || hBLen <= 0) return (hP - path) + 6;
813  hP += 6;
814 
815  if (!XrdNetUtils::Parse(hP, &hName, &hNend, &hPort, &hPend)) return 0;
816  if (*hNend == ']') hNend++;
817  else {if (!(*hNend) && !(hNend = index(hName, '/'))) return 0;
818  if (!(*hPend)) hPend = hNend;
819  }
820 
821  if (pTrim || !(*hPort)) n = hNend - hP;
822  else n = hPend - hP;
823  if (n >= hBLen) return 0;
824  strncpy(hBuff, hP, n);
825  hBuff[n] = 0;
826 
827  if (port)
828  {if (*hNend != ':') *port = 0;
829  else {*port = strtol(hPort, &eP, 10);
830  if (*port < 0 || *port > 65535 || eP != hPend) return 0;
831  }
832  }
833 
834  return hPend-path;
835 }
836 
837 /******************************************************************************/
838 /* L o g 2 */
839 /******************************************************************************/
840 
841 // Based on an algorithm produced by Todd Lehman. However, this one returns 0
842 // when passed 0 (which is invalid). The caller must check the validity of
843 // the input prior to calling Log2(). Essentially, the algorithm subtracts
844 // away progressively smaller squares in the sequence
845 // { 0 <= k <= 5: 2^(2^k) } = { 2**32, 2**16, 2**8 2**4 2**2, 2**1 } =
846 // = { 4294967296, 65536, 256, 16, 4, 2 }
847 // and sums the exponents k of the subtracted values. It is generally the
848 // fastest way to compute log2 for a wide range of possible input values.
849 
850 int XrdOucUtils::Log2(unsigned long long n)
851 {
852  int i = 0;
853 
854  #define SHFT(k) if (n >= (1ULL << k)) { i += k; n >>= k; }
855 
856  SHFT(32); SHFT(16); SHFT(8); SHFT(4); SHFT(2); SHFT(1); return i;
857 
858  #undef SHFT
859 }
860 
861 /******************************************************************************/
862 /* L o g 1 0 */
863 /******************************************************************************/
864 
865 int XrdOucUtils::Log10(unsigned long long n)
866 {
867  int i = 0;
868 
869  #define SHFT(k, m) if (n >= m) { i += k; n /= m; }
870 
871  SHFT(16,10000000000000000ULL); SHFT(8,100000000ULL);
872  SHFT(4,10000ULL); SHFT(2,100ULL); SHFT(1,10ULL);
873  return i;
874 
875  #undef SHFT
876 }
877 
878 /******************************************************************************/
879 /* m a k e H o m e */
880 /******************************************************************************/
881 
882 void XrdOucUtils::makeHome(XrdSysError &eDest, const char *inst)
883 {
884  char buff[2048];
885 
886  if (!inst || !getcwd(buff, sizeof(buff))) return;
887 
888  strcat(buff, "/"); strcat(buff, inst);
889  if (MAKEDIR(buff, pathMode) && errno != EEXIST)
890  {eDest.Emsg("Config", errno, "create home directory", buff);
891  return;
892  }
893 
894  if (chdir(buff) < 0)
895  eDest.Emsg("Config", errno, "chdir to home directory", buff);
896 }
897 
898 /******************************************************************************/
899 
900 bool XrdOucUtils::makeHome(XrdSysError &eDest, const char *inst,
901  const char *path, mode_t mode)
902 {
903  char cwDir[2048];
904  const char *slash = "", *slash2 = "";
905  int n, rc;
906 
907 // Provide backward compatibility for instance name qualification
908 //
909 
910  if (!path || !(n = strlen(path)))
911  {if (inst) makeHome(eDest, inst);
912  return true;
913  }
914 
915 // Augment the path with instance name, if need be
916 //
917  if (path[n-1] != '/') slash = "/";
918  if (!inst || !(n = strlen(inst))) inst = "";
919  else slash2 = "/";
920  n = snprintf(cwDir, sizeof(cwDir), "%s%s%s%s", path, slash, inst, slash2);
921  if (n >= (int)sizeof(cwDir))
922  {eDest.Emsg("Config", ENAMETOOLONG, "create home directory", cwDir);
923  return false;
924  }
925 
926 // Create the path if it doesn't exist
927 //
928  if ((rc = makePath(cwDir, mode, true)))
929  {eDest.Emsg("Config", rc, "create home directory", cwDir);
930  return false;
931  }
932 
933 // Switch to this directory
934 //
935  if (chdir(cwDir) < 0)
936  {eDest.Emsg("Config", errno, "chdir to home directory", cwDir);
937  return false;
938  }
939 
940 // All done
941 //
942  return true;
943 }
944 
945 /******************************************************************************/
946 /* m a k e P a t h */
947 /******************************************************************************/
948 
949 int XrdOucUtils::makePath(char *path, mode_t mode, bool reset)
950 {
951  char *next_path = path+1;
952  struct stat buf;
953  bool dochmod = false; // The 1st component stays as is
954 
955 // Typically, the path exists. So, do a quick check before launching into it
956 //
957  if (!reset && !stat(path, &buf)) return 0;
958 
959 // Start creating directories starting with the root
960 //
961  while((next_path = index(next_path, int('/'))))
962  {*next_path = '\0';
963  if (MAKEDIR(path, mode))
964  if (errno != EEXIST) return -errno;
965  if (dochmod) CHMOD(path, mode);
966  dochmod = reset;
967  *next_path = '/';
968  next_path = next_path+1;
969  }
970 
971 // All done
972 //
973  return 0;
974 }
975 
976 /******************************************************************************/
977 /* m o d e 2 m a s k */
978 /******************************************************************************/
979 
980 bool XrdOucUtils::mode2mask(const char *mode, mode_t &mask)
981 {
982  mode_t mval[3] = {0}, mbit[3] = {0x04, 0x02, 0x01};
983  const char *mok = "rwx";
984  char mlet;
985 
986 // Accept octal mode
987 //
988  if (isdigit(*mode))
989  {char *eP;
990  mask = strtol(mode, &eP, 8);
991  return *eP == 0;
992  }
993 
994 // Make sure we have the correct number of characters
995 //
996  int n = strlen(mode);
997  if (!n || n > 9 || n/3*3 != n) return false;
998 
999 // Convert groups of three
1000 //
1001  int k = 0;
1002  do {for (int i = 0; i < 3; i++)
1003  {mlet = *mode++;
1004  if (mlet != '-')
1005  {if (mlet != mok[i]) return false;
1006  mval[k] |= mbit[i];
1007  }
1008  }
1009  } while(++k < 3 && *mode);
1010 
1011 // Combine the modes and return success
1012 //
1013  mask = mval[0]<<6 | mval[1]<<3 | mval[2];
1014  return true;
1015 }
1016 
1017 /******************************************************************************/
1018 /* p a r s e L i b */
1019 /******************************************************************************/
1020 
1022  const char *libName, char *&libPath, char **libParm)
1023 {
1024  char *val, parms[2048];
1025 
1026 // Get the next token
1027 //
1028  val = Config.GetWord();
1029 
1030 // We do not support stacking as the caller does not support stacking
1031 //
1032  if (val && !strcmp("++", val))
1033  {eDest.Say("Config warning: stacked plugins are not supported in "
1034  "this context; directive ignored!");
1035  return true;
1036  }
1037 
1038 // Now skip over any options
1039 //
1040  while(val && *val && *val == '+') val = Config.GetWord();
1041 
1042 // Check if we actually have a path
1043 //
1044  if (!val || !val[0])
1045  {eDest.Emsg("Config", libName, "not specified"); return false;}
1046 
1047 // Record the path
1048 //
1049  if (libPath) free(libPath);
1050  libPath = strdup(val);
1051 
1052 // Handle optional parameter
1053 //
1054  if (!libParm) return true;
1055  if (*libParm) free(*libParm);
1056  *libParm = 0;
1057 
1058 // Record any parms
1059 //
1060  *parms = 0;
1061  if (!Config.GetRest(parms, sizeof(parms)))
1062  {eDest.Emsg("Config", libName, "parameters too long"); return false;}
1063  if (*parms) *libParm = strdup(parms);
1064  return true;
1065 }
1066 
1067 /******************************************************************************/
1068 /* p a r s e H o m e */
1069 /******************************************************************************/
1070 
1072 {
1073  char *pval, *val, *HomePath = 0;
1074 
1075 // Get the path
1076 //
1077  pval = Config.GetWord();
1078  if (!pval || !pval[0])
1079  {eDest.Emsg("Config", "home path not specified"); return 0;}
1080 
1081 // Make sure it's an absolute path
1082 //
1083  if (*pval != '/')
1084  {eDest.Emsg("Config", "home path not absolute"); return 0;}
1085 
1086 // Record the path
1087 //
1088  HomePath = strdup(pval);
1089 
1090 // Get the optional access rights
1091 //
1092  mode = S_IRWXU;
1093  if ((val = Config.GetWord()) && val[0])
1094  {if (!strcmp("group", val)) mode |= (S_IRGRP | S_IXGRP);
1095  else {eDest.Emsg("Config", "invalid home path modifier -", val);
1096  free(HomePath);
1097  return 0;
1098  }
1099  }
1100  return HomePath;
1101 }
1102 
1103 /******************************************************************************/
1104 /* R e L i n k */
1105 /******************************************************************************/
1106 
1107 int XrdOucUtils::ReLink(const char *path, const char *target, mode_t mode)
1108 {
1109  const mode_t AMode = S_IRWXU; // Only us as a default
1110  char pbuff[MAXPATHLEN+64];
1111  int n;
1112 
1113 // Copy the path
1114 //
1115  n = strlen(path);
1116  if (n >= (int)sizeof(pbuff)) return ENAMETOOLONG;
1117  strcpy(pbuff, path);
1118 
1119 // Unlink the target, make the path, and create the symlink
1120 //
1121  unlink(path);
1122  makePath(pbuff, (mode ? mode : AMode));
1123  if (symlink(target, path)) return errno;
1124  return 0;
1125 }
1126 
1127 /******************************************************************************/
1128 /* S a n i t i z e */
1129 /******************************************************************************/
1130 
1131 void XrdOucUtils::Sanitize(char *str, char subc)
1132 {
1133 
1134 // Sanitize string according to POSIX.1-2008 stanadard using only the
1135 // Portable Filename Character Set: a-z A-Z 0-9 ._- with 1st char not being -
1136 //
1137  if (*str)
1138  {if (*str == '-') *str = subc;
1139  else if (*str == ' ') *str = subc;
1140  char *blank = rindex(str, ' ');
1141  if (blank) while(*blank == ' ') *blank-- = 0;
1142  while(*str)
1143  {if (!isalnum(*str) && index("_-.", *str) == 0) *str = subc;
1144  str++;
1145  }
1146  }
1147 }
1148 
1149 /******************************************************************************/
1150 /* s u b L o g f n */
1151 /******************************************************************************/
1152 
1153 char *XrdOucUtils::subLogfn(XrdSysError &eDest, const char *inst, char *logfn)
1154 {
1155  const mode_t lfm = S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH;
1156  char buff[2048], *sp;
1157  int rc;
1158 
1159  if (!inst || !*inst) return logfn;
1160  if (!(sp = rindex(logfn, '/'))) strcpy(buff, "./");
1161  else {*sp = '\0'; strcpy(buff, logfn); strcat(buff, "/");}
1162 
1163  strcat(buff, inst); strcat(buff, "/");
1164 
1165  if ((rc = XrdOucUtils::makePath(buff, lfm)))
1166  {eDest.Emsg("Config", rc, "create log file path", buff);
1167  return 0;
1168  }
1169 
1170  if (sp) {*sp = '/'; strcat(buff, sp+1);}
1171  else strcat(buff, logfn);
1172 
1173  free(logfn);
1174  return strdup(buff);
1175 }
1176 
1177 /******************************************************************************/
1178 /* t o L o w e r */
1179 /******************************************************************************/
1180 
1181 void XrdOucUtils::toLower(char *str)
1182 {
1183  unsigned char* ustr = (unsigned char*)str; // Avoid undefined behaviour
1184 
1185 // Change each character to lower case
1186 //
1187  while(*ustr) {*ustr = tolower(*ustr); ustr++;}
1188 }
1189 
1190 /******************************************************************************/
1191 /* T o k e n */
1192 /******************************************************************************/
1193 
1194 int XrdOucUtils::Token(const char **str, char delim, char *buff, int bsz)
1195 {
1196  const char *eP, *bP = *str;
1197  int aLen, mLen;
1198 
1199 // Trim off the delimeters. Return zero if nothing left.
1200 //
1201  while(*bP && *bP == delim) bP++;
1202  if (*bP == 0) {*buff = 0; return 0;}
1203 
1204 // Find the next delimiter
1205 //
1206  eP = bP;
1207  while(*eP && *eP != delim) eP++;
1208 
1209 // If we ended at a null, make sure next call will return zero
1210 //
1211  if (*eP == 0) *str = eP;
1212  else *str = eP+1;
1213 
1214 // Calculate length and make sure we don't overrun the buffer
1215 //
1216  aLen = eP-bP;
1217  if (aLen >= bsz) mLen = bsz-1;
1218  else mLen = aLen;
1219 
1220 // Copy token into buffer and end with null byte
1221 //
1222  strncpy(buff, bP, mLen);
1223  buff[mLen] = 0;
1224 
1225 // Return actual length
1226 //
1227  return aLen;
1228 }
1229 
1230 /******************************************************************************/
1231 /* U n d e r c o v e r */
1232 /******************************************************************************/
1233 #ifdef WIN32
1234 void XrdOucUtils::Undercover(XrdSysError &, int, int *)
1235 {
1236 }
1237 #else
1238 void XrdOucUtils::Undercover(XrdSysError &eDest, int noLog, int *pipeFD)
1239 {
1240  static const int maxFiles = 256;
1241  pid_t mypid;
1242  int myfd, logFD = eDest.baseFD();
1243 
1244 // Issue warning if there is no logfile attached
1245 //
1246  if (noLog) eDest.Emsg("Config", "Warning! No log file specified; "
1247  "backgrounding disables all logging!");
1248 
1249 // Fork so that we are not tied to a shell
1250 //
1251  if ((mypid = fork()) < 0)
1252  {eDest.Emsg("Config", errno, "fork process 1 for backgrounding");
1253  return;
1254  }
1255  else if (mypid)
1256  {
1257  // we have been given a pair of pipe descriptors to be able to read the
1258  // status of the child process
1259  if( pipeFD )
1260  {
1261  int status = 1;
1262  close( pipeFD[1] );
1263  // read will wait untill the status is communicated by the
1264  // child process, if the child process dies before being able
1265  // to comunicate the status then read will see EOF
1266  if( read( pipeFD[0], &status, sizeof(status) ) != sizeof(status) )
1267  _exit(1);
1268  _exit(status);
1269  }
1270  // no pipes given, return success
1271  else _exit(0);
1272  }
1273 
1274  if( pipeFD )
1275  close( pipeFD[0] );
1276 
1277 // Become the process group leader
1278 //
1279  if (setsid() < 0)
1280  {eDest.Emsg("Config", errno, "doing setsid() for backgrounding");
1281  return;
1282  }
1283 
1284 // Fork to that we are cannot get a controlling terminal
1285 //
1286  if ((mypid = fork()) < 0)
1287  {eDest.Emsg("Config", errno, "fork process 2 for backgrounding");
1288  return;
1289  }
1290  else if (mypid) _exit(0);
1291 
1292 // Switch stdin, stdout, and stderr to /dev/null (we can't use /dev/console
1293 // unless we are root which is unlikely).
1294 //
1295  if ((myfd = open("/dev/null", O_RDWR)) < 0)
1296  {eDest.Emsg("Config", errno, "open /dev/null for backgrounding");
1297  return;
1298  }
1299  dup2(myfd, 0); dup2(myfd, 1); dup2(myfd, 2); dup2(myfd, logFD);
1300 
1301 // Close any open file descriptors left open by the parent process
1302 // but the communication pipe and the logger's shadow file descriptor.
1303 //
1304  for (myfd = 3; myfd < maxFiles; myfd++)
1305  if( (!pipeFD || myfd != pipeFD[1]) && myfd != logFD ) close(myfd);
1306 }
1307 
1308 /******************************************************************************/
1309 /* U i d N a m e */
1310 /******************************************************************************/
1311 
1312 int XrdOucUtils::UidName(uid_t uID, char *uName, int uNsz, time_t keepT)
1313 {
1314  struct passwd *pEnt, pStruct;
1315  char pBuff[1024];
1316  int n, rc;
1317 
1318 // Get ID from cache, if allowed
1319 //
1320  if (keepT)
1321  {int n = LookUp(uidMap, static_cast<unsigned int>(uID),uName,uNsz);
1322  if (n > 0) return (n < uNsz ? n : 0);
1323  }
1324 
1325 // Try to obtain the username. We use this form to make sure we are using
1326 // the standards conforming version (compilation error otherwise).
1327 //
1328  rc = getpwuid_r(uID, &pStruct, pBuff, sizeof(pBuff), &pEnt);
1329  if (rc || !pEnt)
1330  {n = snprintf(uName, uNsz, "%ud", static_cast<unsigned int>(uID));
1331  return (n >= uNsz ? 0 : n);
1332  }
1333 
1334 // Add entry to the cache if need be
1335 //
1336  if (keepT)
1337  AddID(uidMap, static_cast<unsigned int>(uID), pEnt->pw_name, keepT);
1338 
1339 // Return length of username or zero if it is too big
1340 //
1341  n = strlen(pEnt->pw_name);
1342  if (uNsz <= (int)strlen(pEnt->pw_name)) return 0;
1343  strcpy(uName, pEnt->pw_name);
1344  return n;
1345 }
1346 
1347 /******************************************************************************/
1348 /* U s e r N a m e */
1349 /******************************************************************************/
1350 
1351 int XrdOucUtils::UserName(uid_t uID, char *uName, int uNsz)
1352 {
1353  struct passwd *pEnt, pStruct;
1354  char pBuff[1024];
1355  int rc;
1356 
1357 // Try to obtain the username. We use this form to make sure we are using
1358 // the standards conforming version (compilation error otherwise).
1359 //
1360  rc = getpwuid_r(uID, &pStruct, pBuff, sizeof(pBuff), &pEnt);
1361  if (rc) return rc;
1362  if (!pEnt) return ESRCH;
1363 
1364 // Return length of username or zero if it is too big
1365 //
1366  if (uNsz <= (int)strlen(pEnt->pw_name)) return ENAMETOOLONG;
1367  strcpy(uName, pEnt->pw_name);
1368  return 0;
1369 }
1370 
1371 /******************************************************************************/
1372 /* V a l P a t h */
1373 /******************************************************************************/
1374 
1375 const char *XrdOucUtils::ValPath(const char *path, mode_t allow, bool isdir)
1376 {
1377  static const mode_t mMask = S_IRWXU | S_IRWXG | S_IRWXO;
1378  struct stat buf;
1379 
1380 // Check if this really exists
1381 //
1382  if (stat(path, &buf))
1383  {if (errno == ENOENT) return "does not exist.";
1384  return XrdSysE2T(errno);
1385  }
1386 
1387 // Verify that this is the correct type of file
1388 //
1389  if (isdir)
1390  {if (!S_ISDIR(buf.st_mode)) return "is not a directory.";
1391  } else {
1392  if (!S_ISREG(buf.st_mode)) return "is not a file.";
1393  }
1394 
1395 // Verify that the does not have excessive privileges
1396 //
1397  if ((buf.st_mode & mMask) & ~allow) return "has excessive access rights.";
1398 
1399 // All went well
1400 //
1401  return 0;
1402 }
1403 
1404 /******************************************************************************/
1405 /* P i d F i l e */
1406 /******************************************************************************/
1407 
1408 bool XrdOucUtils::PidFile(XrdSysError &eDest, const char *path)
1409 {
1410  char buff[32];
1411  int fd;
1412 
1413  if( (fd = open( path, O_WRONLY|O_CREAT|O_TRUNC, 0644 )) < 0 )
1414  {
1415  eDest.Emsg( "Config", errno, "create pidfile" );
1416  return false;
1417  }
1418 
1419  if( write( fd, buff, snprintf( buff, sizeof(buff), "%d",
1420  static_cast<int>(getpid()) ) ) < 0 )
1421  {
1422  eDest.Emsg( "Config", errno, "write to pidfile" );
1423  close(fd);
1424  return false;
1425  }
1426 
1427  close(fd);
1428  return true;
1429 }
1430 /******************************************************************************/
1431 /* getModificationTime */
1432 /******************************************************************************/
1433 int XrdOucUtils::getModificationTime(const char *path, time_t &modificationTime) {
1434  struct stat buf;
1435  int statRet = ::stat(path,&buf);
1436  if(!statRet) {
1437  modificationTime = buf.st_mtime;
1438  }
1439  return statRet;
1440 }
1441 
1442 void XrdOucUtils::trim(std::string &str) {
1443  // Trim leading non-letters
1444  while( str.size() && !isgraph(str[0]) ) str.erase(str.begin());
1445 
1446  // Trim trailing non-letters
1447 
1448  while( str.size() && !isgraph(str[str.size()-1]) )
1449  str.resize (str.size () - 1);
1450 }
1451 
1457 static bool is_token_character(int c)
1458 {
1459  if (isalnum(c))
1460  return true;
1461 
1462  static constexpr char token_chars[] = "-._~+/=:";
1463 
1464  for (char ch : token_chars)
1465  if (c == ch)
1466  return true;
1467 
1468  return false;
1469 }
1470 
1479 std::string obfuscateAuth(const std::string& input)
1480 {
1481  static const regex_t auth_regex = []() {
1482  constexpr char re[] =
1483  "(access_token=|authz=|(transferheader)?(www-|proxy-)?auth(orization|enticate)[[:space:]]*:[[:space:]]*)"
1484  "(Bearer([[:space:]]|%20)?(token([[:space:]]|%20)?)?)?";
1485 
1486  regex_t regex;
1487 
1488  if (regcomp(&regex, re, REG_EXTENDED | REG_ICASE) != 0)
1489  throw std::runtime_error("Failed to compile regular expression");
1490 
1491  return regex;
1492  }();
1493 
1494  regmatch_t match;
1495  size_t offset = 0;
1496  std::string redacted;
1497  const char *const text = input.c_str();
1498 
1499  while (regexec(&auth_regex, text + offset, 1, &match, 0) == 0) {
1500  redacted.append(text + offset, match.rm_eo).append("REDACTED");
1501 
1502  offset += match.rm_eo;
1503 
1504  while (offset < input.size() && is_token_character(input[offset]))
1505  ++offset;
1506  }
1507 
1508  return redacted.append(text + offset);
1509 }
1510 
1511 #endif
struct stat Stat
Definition: XrdCks.cc:49
static XrdSysError eDest(0,"crypto_")
uint32_t crc32c(uint32_t crc, void const *buf, size_t len)
#define ENODATA
Definition: XrdOucUtils.cc:66
#define SHFT(k)
static bool is_token_character(int c)
std::string obfuscateAuth(const std::string &input)
int stat(const char *path, struct stat *buf)
int open(const char *path, int oflag,...)
int fstat(int fildes, struct stat *buf)
int chdir(const char *path)
int unlink(const char *path)
ssize_t write(int fildes, const void *buf, size_t nbyte)
ssize_t read(int fildes, void *buf, size_t nbyte)
#define close(a)
Definition: XrdPosix.hh:48
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104
size_t strlcpy(char *dst, const char *src, size_t sz)
#define CHMOD(path, mode)
#define MAKEDIR(path, mode)
static bool Match(const char *hName, const char *pattern)
Definition: XrdNetUtils.cc:623
static bool Parse(const char *hSpec, const char **hName, const char **hNend, const char **hPort, const char **hPend)
Definition: XrdNetUtils.cc:745
static uint32_t Calc32C(const void *data, size_t count, uint32_t prevcs=0)
Definition: XrdOucCRC.cc:190
char * Get(const char *varname)
Definition: XrdOucEnv.hh:69
MDLen
SHA3 digest lengths (bits to bytes).
Definition: XrdOucSHA3.hh:56
static void * Calc(const void *in, size_t inlen, void *md, MDLen mdlen)
Definition: XrdOucSHA3.cc:150
const char * c_str() const
int length() const
int tokenize(XrdOucString &tok, int from, char del=':')
static char * parseHome(XrdSysError &eDest, XrdOucStream &Config, int &mode)
static void Sanitize(char *instr, char subc='_')
static bool getGID(const char *gName, gid_t &gID)
Definition: XrdOucUtils.cc:514
static const mode_t pathMode
Definition: XrdOucUtils.hh:47
static const char * HSize(size_t bytes, char *buff, int bsz)
Definition: XrdOucUtils.cc:634
static int isFWD(const char *path, int *port=0, char *hBuff=0, int hBLen=0, bool pTrim=false)
Definition: XrdOucUtils.cc:802
static int UserName(uid_t uID, char *uName, int uNsz)
static char * Ident(long long &mySID, char *iBuff, int iBlen, const char *iHost, const char *iProg, const char *iName, int Port)
Definition: XrdOucUtils.cc:734
static bool getUID(const char *uName, uid_t &uID, gid_t *gID=0)
Definition: XrdOucUtils.cc:530
static int getModificationTime(const char *path, time_t &modificationTime)
static const char * ValPath(const char *path, mode_t allow, bool isdir)
static char * genPath(const char *path, const char *inst, const char *psfx=0)
Definition: XrdOucUtils.cc:418
static int Token(const char **str, char delim, char *buff, int bsz)
static int ReLink(const char *path, const char *target, mode_t mode=0)
static bool parseLib(XrdSysError &eDest, XrdOucStream &Config, const char *libName, char *&path, char **libparm)
static int is1of(char *val, const char **clist)
Definition: XrdOucUtils.cc:790
static const char * InstName(int TranOpt=0)
Definition: XrdOucUtils.cc:764
static char * eText(int rc, char *eBuff, int eBlen)
Definition: XrdOucUtils.cc:198
static int argList(char *args, char **argV, int argC)
Definition: XrdOucUtils.cc:125
static bool mode2mask(const char *mode, mode_t &mask)
Definition: XrdOucUtils.cc:980
static int GidName(gid_t gID, char *gName, int gNsz, time_t keepT=0)
Definition: XrdOucUtils.cc:548
static int UidName(uid_t uID, char *uName, int uNsz, time_t keepT=0)
static char * bin2hex(char *inbuff, int dlen, char *buff, int blen, bool sep=true)
Definition: XrdOucUtils.cc:164
static int Log10(unsigned long long n)
Definition: XrdOucUtils.cc:865
static int doIf(XrdSysError *eDest, XrdOucStream &Config, const char *what, const char *hname, const char *nname, const char *pname)
Definition: XrdOucUtils.cc:232
static int makePath(char *path, mode_t mode, bool reset=false)
Definition: XrdOucUtils.cc:949
static int GroupName(gid_t gID, char *gName, int gNsz)
Definition: XrdOucUtils.cc:597
static void trim(std::string &str)
static bool findPgm(const char *pgm, XrdOucString &path)
Definition: XrdOucUtils.cc:355
static bool PidFile(XrdSysError &eDest, const char *path)
static const char * i2bstr(char *buff, int blen, int val, bool pad=false)
Definition: XrdOucUtils.cc:665
static void toLower(char *str)
static int fmtBytes(long long val, char *buff, int bsz)
Definition: XrdOucUtils.cc:391
static int Log2(unsigned long long n)
Definition: XrdOucUtils.cc:850
static void makeHome(XrdSysError &eDest, const char *inst)
Definition: XrdOucUtils.cc:882
static bool endsWith(const char *text, const char *ending, int endlen)
Definition: XrdOucUtils.cc:184
static void Undercover(XrdSysError &eDest, int noLog, int *pipeFD=0)
static char * getFile(const char *path, int &rc, int maxsz=10240, bool notempty=true)
Definition: XrdOucUtils.cc:457
static char * subLogfn(XrdSysError &eDest, const char *inst, char *logfn)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)
Definition: XrdSysError.cc:141
int baseFD()
Definition: XrdSysError.cc:73
XrdCmsConfig Config
XrdOucEnv theEnv