XRootD
XrdSutAux.cc File Reference
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <cerrno>
#include <ctime>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "XrdSys/XrdSysLogger.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysPwd.hh"
#include "XrdOuc/XrdOucString.hh"
#include "XrdSut/XrdSutAux.hh"
#include "XrdSut/XrdSutRndm.hh"
#include "XrdSut/XrdSutTrace.hh"
+ Include dependency graph for XrdSutAux.cc:

Go to the source code of this file.

Functions

bool XrdSutAskConfirm (const char *msg1, bool defact, const char *msg2)
 
const char * XrdSutBuckStr (int kbck)
 
int XrdSutExpand (XrdOucString &path)
 
int XrdSutFromHex (const char *in, char *out, int &lout)
 
int XrdSutGetLine (XrdOucString &line, const char *prompt)
 
int XrdSutGetPass (const char *prompt, XrdOucString &passwd)
 
const char * XrdSutHome ()
 
volatile void * XrdSutMemSet (volatile void *dst, int c, int len)
 
int XrdSutMkdir (const char *dir, unsigned int mode, const char *opt)
 
int XrdSutParseTime (const char *tstr, int opt)
 
int XrdSutResolve (XrdOucString &path, const char *ho, const char *vo, const char *gr, const char *us)
 
void XrdSutSetTrace (kXR_int32 trace)
 
int XrdSutTimeString (int t, char *st, int opt)
 
int XrdSutToHex (const char *in, int lin, char *out)
 

Variables

static XrdSysError eDest (0,"sut_")
 
static const char * gXRSBucketTypes []
 
static XrdSysLogger Logger
 
XrdOucTracesutTrace = 0
 

Function Documentation

◆ XrdSutAskConfirm()

bool XrdSutAskConfirm ( const char *  msg1,
bool  defact,
const char *  msg2 
)

Definition at line 203 of file XrdSutAux.cc.

204 {
205  // Prompt for confirmation of action
206  // If defined, msg1 is printed as prompt, followed by the default action
207  // ( [y] == do-act, for defact = true;
208  // [n] == do-not-act, for defact = false)
209  // If defined, msg2 is printed before prompting.
210 
211  bool rc = defact;
212 
213  if (msg2)
214  std::cout << msg2;
215  XrdOucString ask;
216  XrdOucString prompt = defact ? " [y]: " : " [n]: ";
217  if (msg1)
218  prompt.insert(msg1,0);
219  XrdSutGetLine(ask,prompt.c_str());
220  ask.lower(0);
221  if (ask.length()) {
222  if (defact && (ask == 'n' || ask == "no")) {
223  rc = 0;
224  } else if (!defact && (ask == 'y' || ask == "yes")) {
225  rc = 1;
226  }
227  }
228  // we are done
229  return rc;
230 }
int XrdSutGetLine(XrdOucString &line, const char *prompt)
Definition: XrdSutAux.cc:179
void insert(const int i, int start=-1)
const char * c_str() const
int length() const
void lower(int pos, int size=0)

References XrdOucString::c_str(), XrdOucString::insert(), XrdOucString::length(), XrdOucString::lower(), and XrdSutGetLine().

+ Here is the call graph for this function:

◆ XrdSutBuckStr()

const char* XrdSutBuckStr ( int  kbck)

Definition at line 121 of file XrdSutAux.cc.

122 {
123  // Return bucket string
124  static const char *ukn = "Unknown";
125 
126  kbck = (kbck < 0) ? 0 : kbck;
127  kbck = (kbck > kXRS_reserved) ? 0 : kbck;
128  kbck = (kbck >= kXRS_cryptomod) ? (kbck - kXRS_cryptomod + 2) : kbck;
129 
130  if (kbck < 0 || kbck > (kXRS_reserved - kXRS_cryptomod + 2))
131  return ukn;
132  else
133  return gXRSBucketTypes[kbck];
134 }
static const char * gXRSBucketTypes[]
Definition: XrdSutAux.cc:49
@ kXRS_reserved
Definition: XrdSutAux.hh:85
@ kXRS_cryptomod
Definition: XrdSutAux.hh:57

References gXRSBucketTypes, kXRS_cryptomod, and kXRS_reserved.

Referenced by XrdSutBuffer::XrdSutBuffer(), XrdSecProtocolgsi::Authenticate(), XrdSutBucket::Dump(), XrdSecProtocolgsi::getCredentials(), XrdSecProtocolpwd::getCredentials(), XrdSutBuffer::MarshalBucket(), and XrdSutBuffer::UnmarshalBucket().

+ Here is the caller graph for this function:

◆ XrdSutExpand()

int XrdSutExpand ( XrdOucString path)

Definition at line 360 of file XrdSutAux.cc.

361 {
362  // Expand '~' or $PWD for incomplete absolute path specification
363  // Returns 0 in case of success, -EINVAL if path is not defined;
364  // -errno if failure of the pwnam functions; -ENOENT if PWD is not
365  // defined
366  EPNAME("Expand");
367 
368  // Path must be defined
369  if (!path.length())
370  return -EINVAL;
371 
372  // If path is absolute, do nothing
373  if (path[0] == '/')
374  return 0;
375 
376  if (path[0] == '~') {
377  XrdOucString unam, home;
378  XrdOucString sdir(path);
379  int iu = path.find('/');
380  if (iu != STR_NPOS) {
381  if (iu > 1)
382  unam.assign(path, 1, iu-1);
383  sdir.erase(0, iu);
384  } else
385  sdir = '/';
386  if (unam.length() > 0) {
387  struct passwd *pw;
388  XrdSysPwd thePwd(unam.c_str(), &pw);
389  if (!pw) {
390  DEBUG("cannot pwnam information for local user "<<
391  ((unam.length() > 0) ? unam : XrdOucString("")));
392  return -errno;
393  }
394  home = pw->pw_dir;
395  } else
396  home = XrdSutHome();
397  if (home.length() > 0) {
398  sdir.insert(home.c_str(),0);
399  path = sdir;
400  }
401  } else {
402  // relative path, add local dir
403  char *pwd = getenv("PWD");
404  if (pwd) {
405  path.insert('/',0);
406  path.insert(pwd,0);
407  path.erase("//");
408  } else {
409  DEBUG("PWD undefined ");
410  return -ENOENT;
411  }
412  }
413  return 0;
414 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
#define STR_NPOS
const char * XrdSutHome()
Definition: XrdSutAux.cc:459
void assign(const char *s, int j, int k=-1)
int erase(int start=0, int size=0)
int find(const char c, int start=0, bool forward=1)

References XrdOucString::assign(), XrdOucString::c_str(), DEBUG, EPNAME, XrdOucString::erase(), XrdOucString::find(), XrdOucString::insert(), XrdOucString::length(), STR_NPOS, and XrdSutHome().

Referenced by XrdSecProtocolgsi::Init(), XrdSecProtocolpwd::Init(), ParseArguments(), and XrdSutMkdir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutFromHex()

int XrdSutFromHex ( const char *  in,
char *  out,
int &  lout 
)

Definition at line 268 of file XrdSutAux.cc.

269 {
270  // Content of the hexadecimal, null-terminated, string at in, is
271  // transformed into lout bytes returned in out.
272  // The output buffer should be allocated by the caller to contain
273  // at least lin/2 bytes if lin=strlen(in) is even, and lin/2+1 bytes
274  // if lin is odd (in this case an additional char equal 0 is appended
275  // to in).
276  // Return 0 in case of success, -1 in case of error (errno set to EINVAL if
277  // any of in or out are not defined).
278 
279  lout = 0;
280  if (!in || !out) {
281  errno = EINVAL;
282  return -1;
283  }
284 
285  int lin = strlen(in);
286  char st[3] = {0};
287  int i = 0, k = 0;
288  for ( ; i<lin; i += 2) {
289  st[0] = in[i];
290  st[1] = ((i+1) < lin) ? in[i+1] : 0;
291  unsigned int c;
292  sscanf(st,"%x",&c);
293  out[k++] = (char)(0x000000FF & c);
294  }
295 
296  lout = k;
297 
298  return 0;
299 }

Referenced by XrdCryptoBasic::FromHex().

+ Here is the caller graph for this function:

◆ XrdSutGetLine()

int XrdSutGetLine ( XrdOucString line,
const char *  prompt 
)

Definition at line 179 of file XrdSutAux.cc.

180 {
181  // Get line from main input stream.
182  // Prompt 'prompt' if this is defined.
183  // Returns number of chars entered.
184  // NB: at most XrdSutMAXBUF-1 chars will be accepted
185  char bin[XrdSutMAXBUF] = {0};
186 
187  // Print prompt, if requested
188  if (prompt)
189  std::cout << prompt;
190 
191  // Get line
192  std::cin.getline(bin,XrdSutMAXBUF-1);
193 
194  // Fill input
195  line = bin;
196 
197  return line.length();
198 }
#define XrdSutMAXBUF
Definition: XrdSutAux.hh:48

References XrdOucString::length(), and XrdSutMAXBUF.

Referenced by AskConfirm(), main(), and XrdSutAskConfirm().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutGetPass()

int XrdSutGetPass ( const char *  prompt,
XrdOucString passwd 
)

Definition at line 150 of file XrdSutAux.cc.

151 {
152  // Get password from command line using getpass
153  // *** Use only if you cannot provide a better alternative ***
154  // User will be prompted for 'prompt'; the entered password
155  // is returned in 'passwd'.
156  // Returns 0 if ok, -1 if any error occurs.
157  EPNAME("GetPass");
158 
159  char *pw = getpass(prompt);
160  if (pw) {
161  // Get rid of special chars, if any
162  int k = 0, i = 0, len = strlen(pw);
163  for (; i<len ; i++)
164  if (pw[i] > 0x20) pw[k++] = pw[i];
165  pw[k] = 0;
166  passwd = pw;
167  XrdSutMemSet((void *)pw,0,len);
168  } else {
169  DEBUG("error from getpass");
170  return -1;
171  }
172  return 0;
173 }
volatile void * XrdSutMemSet(volatile void *dst, int c, int len)
Definition: XrdSutAux.cc:140

References DEBUG, EPNAME, and XrdSutMemSet().

Referenced by AddPassword().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutHome()

const char* XrdSutHome ( )

Definition at line 459 of file XrdSutAux.cc.

460 {
461  // Gets the home directory preferentially from HOME or from pwd entry
462  EPNAME("Home");
463 
464  // Use the save value, if any
465  static XrdOucString homedir;
466  if (homedir.length() <= 0) {
467  // Check the HOME environment variable
468  if (getenv("HOME"))
469  homedir = getenv("HOME");
470  if (homedir.length() <= 0) {
471  struct passwd *pw;
472  XrdSysPwd thePwd(getuid(), &pw);
473  if (pw) homedir = pw->pw_dir;
474  }
475  if (homedir.length() <= 0)
476  DEBUG("Warning: home directory undefined! ");
477  }
478 
479  // Done
480  return homedir.c_str();
481 }

References XrdOucString::c_str(), DEBUG, EPNAME, and XrdOucString::length().

Referenced by XrdSecProtocolgsi::Init(), XrdSecProtocolpwd::Init(), ParseArguments(), and XrdSutExpand().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutMemSet()

volatile void* XrdSutMemSet ( volatile void *  dst,
int  c,
int  len 
)

Definition at line 140 of file XrdSutAux.cc.

141 {
142  return memset((void*)dst, c, len);
143 }

Referenced by XrdSutGetPass().

+ Here is the caller graph for this function:

◆ XrdSutMkdir()

int XrdSutMkdir ( const char *  dir,
unsigned int  mode,
const char *  opt 
)

Definition at line 487 of file XrdSutAux.cc.

488 {
489  // Make directory dir
490  // mode specifies permissions
491  // opt == "-p" : make parent directories as needed
492 
493  if (!dir) {
494  errno = EINVAL;
495  return -1;
496  }
497 
498  if (!strncmp(opt,"-p",2)) {
499  //
500  // make also parent directories, if needed
501  XrdOucString dd(dir);
502  XrdSutExpand(dd);
503  if (dd[dd.length()-1] != '/')
504  dd.append('/');
505  int lsl = dd.find('/',1);
506  while (lsl > -1) {
507  XrdOucString pd(dd,0,lsl-1);
508  struct stat st;
509  if (stat(pd.c_str(),&st) == -1) {
510  if (errno == ENOENT) {
511  // path does not exists: create it
512  if (mkdir(pd.c_str(),mode) != 0)
513  return -1;
514  } else {
515  return -1;
516  }
517  }
518  // Go to next
519  lsl = dd.find('/',lsl+1);
520  }
521 
522  } else {
523  return mkdir(dir,mode);
524  }
525 
526  return 0;
527 }
int stat(const char *path, struct stat *buf)
int mkdir(const char *path, mode_t mode)
int XrdSutExpand(XrdOucString &path)
Definition: XrdSutAux.cc:360

References XrdOucString::append(), XrdOucString::c_str(), XrdOucString::find(), XrdOucString::length(), mkdir(), stat(), and XrdSutExpand().

Referenced by XrdSecProtocolpwd::Init(), ParseArguments(), SavePasswd(), and SavePuk().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutParseTime()

int XrdSutParseTime ( const char *  tstr,
int  opt 
)

Definition at line 534 of file XrdSutAux.cc.

535 {
536  // Parse time string of the form "<val1><unit1>:<val2><unit2>:..."
537  // with <val> any integer and <unit> one of the following chars:
538  // 'y' for years
539  // 'd' for days
540  // 'h' for hours
541  // 'm' for minutes
542  // 's' for seconds
543  // (e.g. "34d:10h:20s")
544  // If opt == 1, assume a string in the form ".hh"[:<ss>[:<mm>]]"
545  // (e.g. "12:24:35" for 12 hours, 24 minutes and 35 secs)
546  // Return the corresponding number of seconds
547  EPNAME("ParseTime");
548 
549  XrdOucString ts = tstr;
550  XrdOucString fr = "";
551  int i = 0;
552  int tsec = 0;
553  // Parse list
554  if (ts.length()) {
555  int ls = 0;
556  int ld = ts.find(':',1);
557  ld = (ld == -1) ? ts.length() - 1 : ld;
558  while (ld >= ls) {
559  fr.assign(ts, ls, ld);
560  fr.erase(":");
561  // Check this fraction
562  if (opt == 0) {
563  if (fr.length() > 1) {
564  // The unit must be known
565  char u = fr[fr.length()-1];
566  fr.erase(fr.length()-1);
567  if (u == 'y') {
568  tsec += atoi(fr.c_str())*31536000;
569  } else if (u == 'd') {
570  tsec += atoi(fr.c_str())*86400;
571  } else if (u == 'h') {
572  tsec += atoi(fr.c_str())*3600;
573  } else if (u == 'm') {
574  tsec += atoi(fr.c_str())*60;
575  } else if (u == 's') {
576  tsec += atoi(fr.c_str());
577  } else {
578  DEBUG("unknown unit: "<<u);
579  }
580  } else {
581  DEBUG("Incomplete fraction: "<<fr.c_str());
582  }
583  } else {
584  if (i == 0) {
585  tsec += atoi(fr.c_str())*3600;
586  } else if (i == 1) {
587  tsec += atoi(fr.c_str())*60;
588  } else if (i == 2) {
589  tsec += atoi(fr.c_str());
590  }
591  }
592  i++;
593  ls = ld + 1;
594  ld = ts.find(':',ls);
595  ld = (ld == -1) ? ts.length() - 1 : ld;
596  }
597  }
598  return tsec;
599 }
static std::string ts()
timestamp output for logging messages
Definition: XrdCephOss.cc:53

References XrdOucString::assign(), XrdOucString::c_str(), DEBUG, EPNAME, XrdOucString::erase(), XrdOucString::length(), and ts().

Referenced by main(), and XrdSecProtocolpwdInit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutResolve()

int XrdSutResolve ( XrdOucString path,
const char *  ho,
const char *  vo,
const char *  gr,
const char *  us 
)

Definition at line 419 of file XrdSutAux.cc.

421 {
422  // Resolve templates <host>, <vo>, <group>, <user> (if any)
423  // Returns 0 in case of success, -EINVAL if path is not defined.
424 
425  // Path must be defined
426  if (!path.length())
427  return -EINVAL;
428 
429  // No templates, nothing to do
430  if (path.find("<") == STR_NPOS)
431  return 0;
432 
433  // Replace <host>, if defined
434  if (ho && strlen(ho) > 0) path.replace("<host>", ho);
435 
436  // Replace <vo>, if defined
437  if (vo && strlen(vo) > 0) path.replace("<vo>", vo);
438 
439  // Replace <group>, if defined
440  if (gr && strlen(gr) > 0) path.replace("<group>", gr);
441 
442  // Replace <user>, if defined
443  if (us && strlen(us) > 0) path.replace("<user>", us);
444 
445  // Replace <rtag>, if defined
446  if (path.find("<rtag>") != STR_NPOS) {
447  XrdOucString rtag;
448  XrdSutRndm::GetString(2,6,rtag);
449  path.replace("<rtag>", rtag);
450  }
451 
452  // Done
453  return 0;
454 }
int replace(const char *s1, const char *s2, int from=0, int to=-1)
static int GetString(int opt, int len, XrdOucString &s)
Definition: XrdSutRndm.cc:120

References XrdOucString::find(), XrdSutRndm::GetString(), XrdOucString::length(), XrdOucString::replace(), and STR_NPOS.

+ Here is the call graph for this function:

◆ XrdSutSetTrace()

void XrdSutSetTrace ( kXR_int32  trace)

Definition at line 93 of file XrdSutAux.cc.

94 {
95  // Set trace flags according to 'trace'
96 
97  //
98  // Initiate error logging and tracing
100  if (!sutTrace)
101  sutTrace = new XrdOucTrace(&eDest);
102  if (sutTrace) {
103  // Set debug mask
104  sutTrace->What = 0;
105  // Low level only
106  if ((trace & sutTRACE_Notify))
108  // Medium level
109  if ((trace & sutTRACE_Debug))
111  // High level
112  if ((trace & sutTRACE_Dump))
114  }
115 }
static XrdSysError eDest(0,"sut_")
static XrdSysLogger Logger
Definition: XrdSutAux.cc:85
XrdOucTrace * sutTrace
Definition: XrdSutAux.cc:87
#define sutTRACE_ALL
Definition: XrdSutAux.hh:97
#define sutTRACE_Notify
Definition: XrdSutAux.hh:100
#define sutTRACE_Debug
Definition: XrdSutAux.hh:99
#define sutTRACE_Dump
Definition: XrdSutAux.hh:98
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:141

References eDest, Logger, XrdSysError::logger(), sutTrace, sutTRACE_ALL, sutTRACE_Debug, sutTRACE_Dump, sutTRACE_Notify, and XrdOucTrace::What.

Referenced by XrdSecProtocolgsi::Init(), XrdSecProtocolpwd::Init(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XrdSutTimeString()

int XrdSutTimeString ( int  t,
char *  st,
int  opt 
)

Definition at line 305 of file XrdSutAux.cc.

306 {
307  // Trasform a time in secs since 1Jan1970 in a string of the format
308  // 24Apr2006:09:10:23 (opt = 0, default)
309  // 24Apr2006-091023 (opt = 1)
310  // The buffer st must be supplied by the caller to contain at least 20.
311  // This length is returned when calling the function with t=-1
312  static char month[12][4] = {"Jan","Feb","Mar","Apr","May","Jun",
313  "Jul","Aug","Sep","Oct","Nov","Dec"};
314  static short flen = strlen("24Apr2006:09:10:23");
315 
316  // Check if the length is required
317  if (t == -1)
318  return (flen+1);
319 
320  // Now check inputs
321  if (t < 0 || !st)
322  return -1;
323 
324  // Get the breakdown
325  struct tm tst;
326  time_t ttmp = t;
327  if (!localtime_r(&ttmp,&tst))
328  return -2;
329 
330  // Now fill the output
331  if (opt == 1) {
332  sprintf(st,"%2d%3s%4d-%2d%2d%2d",tst.tm_mday,month[tst.tm_mon],
333  1900+tst.tm_year,
334  tst.tm_hour,tst.tm_min,tst.tm_sec);
335  // Make sure is null terminated at the right point
336  st[flen-2] = '\0';
337  } else {
338  sprintf(st,"%2d%3s%4d:%2d:%2d:%2d",tst.tm_mday,month[tst.tm_mon],
339  1900+tst.tm_year,
340  tst.tm_hour,tst.tm_min,tst.tm_sec);
341  }
342 
343  // Make sure there are no empty spaces
344  if (st[0] == 0x20) st[0] = 0x30;
345  int i = 10;
346  for (; i <= 16; i++ )
347  if (st[i] == 0x20) st[i] = 0x30;
348 
349 
350  // Null termination
351  st[flen] = 0;
352 
353  // Ok
354  return 0;
355 }

Referenced by XrdSutCacheEntry::AsString(), XrdSutPFEntry::AsString(), XrdSutPFile::Browse(), XrdSutPFCache::Dump(), and SavePuk().

+ Here is the caller graph for this function:

◆ XrdSutToHex()

int XrdSutToHex ( const char *  in,
int  lin,
char *  out 
)

Definition at line 235 of file XrdSutAux.cc.

236 {
237  // Content of lin bytes at in are transformed into an hexadecimal,
238  // null-terminated, string of length 2*lin; the result is returned
239  // in the buffer pointed by out, which must be allocated by the caller
240  // to contain at least 2*lin+1 bytes.
241  // Return 0 in case of success, -1 in case of error (errno set to EINVAL if
242  // any of in or out are not defined).
243 
244  if (!in || !out) {
245  errno = EINVAL;
246  return -1;
247  }
248 
249  int lbuf = 2*lin+1;
250  int i = 0;
251  out[0] = 0;
252  for ( ; i < lin; i++)
253  {
254  char buff[3];
255  sprintf(buff, "%02x", (0xFF & in[i]));
256  strncat(out, buff, 3);
257  }
258  // Null termination
259  out[lbuf-1] = 0;
260 
261  // ok
262  return 0;
263 }

Referenced by XrdCryptoBasic::AsHexString(), XrdSecProtocolpwd::Authenticate(), and main().

+ Here is the caller graph for this function:

Variable Documentation

◆ eDest

XrdSysError eDest(0,"sut_") ( ,
"sut_"   
)
static

Referenced by XrdSutSetTrace().

◆ gXRSBucketTypes

const char* gXRSBucketTypes[]
static

Definition at line 49 of file XrdSutAux.cc.

Referenced by XrdSutBuckStr().

◆ Logger

XrdSysLogger Logger
static

Definition at line 85 of file XrdSutAux.cc.

Referenced by XrdSutSetTrace().

◆ sutTrace

XrdOucTrace* sutTrace = 0

Definition at line 87 of file XrdSutAux.cc.

Referenced by XrdSutSetTrace().