XRootD
Loading...
Searching...
No Matches
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 209 of file XrdSutAux.cc.

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

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 366 of file XrdSutAux.cc.

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

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

Referenced by XrdCryptoBasic::FromHex().

+ Here is the caller graph for this function:

◆ XrdSutGetLine()

int XrdSutGetLine ( XrdOucString line,
const char *  prompt 
)

Definition at line 185 of file XrdSutAux.cc.

186{
187 // Get line from main input stream.
188 // Prompt 'prompt' if this is defined.
189 // Returns number of chars entered.
190 // NB: at most XrdSutMAXBUF-1 chars will be accepted
191 char bin[XrdSutMAXBUF] = {0};
192
193 // Print prompt, if requested
194 if (prompt)
195 std::cout << prompt;
196
197 // Get line
198 std::cin.getline(bin,XrdSutMAXBUF-1);
199
200 // Fill input
201 line = bin;
202
203 return line.length();
204}
#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 156 of file XrdSutAux.cc.

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

References DEBUG, EPNAME, and XrdSutMemSet().

Referenced by AddPassword(), and AddPassword().

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

◆ XrdSutHome()

const char * XrdSutHome ( )

Definition at line 465 of file XrdSutAux.cc.

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

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 // To avoid problems due to compiler optmization
143 // Taken from Viega&Messier, "Secure Programming Cookbook", O'Really, #13.2
144 // (see discussion there)
145 volatile char *buf;
146
147 for (buf = (volatile char *)dst; len; buf[--len] = c) {}
148 return dst;
149}

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 493 of file XrdSutAux.cc.

494{
495 // Make directory dir
496 // mode specifies permissions
497 // opt == "-p" : make parent directories as needed
498
499 if (!dir) {
500 errno = EINVAL;
501 return -1;
502 }
503
504 if (!strncmp(opt,"-p",2)) {
505 //
506 // make also parent directories, if needed
507 XrdOucString dd(dir);
508 XrdSutExpand(dd);
509 if (dd[dd.length()-1] != '/')
510 dd.append('/');
511 int lsl = dd.find('/',1);
512 while (lsl > -1) {
513 XrdOucString pd(dd,0,lsl-1);
514 struct stat st;
515 if (stat(pd.c_str(),&st) == -1) {
516 if (errno == ENOENT) {
517 // path does not exists: create it
518 if (mkdir(pd.c_str(),mode) != 0)
519 return -1;
520 } else {
521 return -1;
522 }
523 }
524 // Go to next
525 lsl = dd.find('/',lsl+1);
526 }
527
528 } else {
529 return mkdir(dir,mode);
530 }
531
532 return 0;
533}
#define mkdir(a, b)
Definition XrdPosix.hh:69
#define stat(a, b)
Definition XrdPosix.hh:96
int XrdSutExpand(XrdOucString &path)
Definition XrdSutAux.cc:366

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 540 of file XrdSutAux.cc.

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

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

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 425 of file XrdSutAux.cc.

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

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)

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 311 of file XrdSutAux.cc.

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

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 241 of file XrdSutAux.cc.

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

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.

49 {
50 "kXRS_none",
51 "kXRS_inactive",
52 "kXRS_cryptomod",
53 "kXRS_main",
54 "kXRS_srv_seal",
55 "kXRS_clnt_seal",
56 "kXRS_puk",
57 "kXRS_cipher",
58 "kXRS_rtag",
59 "kXRS_signed_rtag",
60 "kXRS_user",
61 "kXRS_host",
62 "kXRS_creds",
63 "kXRS_message",
64 "kXRS_srvID",
65 "kXRS_sessionID",
66 "kXRS_version",
67 "kXRS_status",
68 "kXRS_localstatus",
69 "kXRS_othercreds",
70 "kXRS_cache_idx",
71 "kXRS_clnt_opts",
72 "kXRS_error_code",
73 "kXRS_timestamp",
74 "kXRS_x509",
75 "kXRS_issuer_hash",
76 "kXRS_x509_req",
77 "kXRS_cipher_alg",
78 "kXRS_md_alg",
79 "kXRS_afsinfo",
80 "kXRS_reserved"
81};

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().