XRootD
XrdNetIdentity.cc File Reference
#include <string.h>
#include <unistd.h>
#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetIdentity.hh"
#include "XrdNet/XrdNetIF.hh"
#include "XrdOuc/XrdOucTList.hh"
#include "XrdOuc/XrdOucUtils.hh"
#include "XrdSys/XrdSysE2T.hh"
+ Include dependency graph for XrdNetIdentity.cc:

Go to the source code of this file.

Functions

static bool getMyFQN ()
 
static void initIdentity ()
 

Variables

static const char * DNS_Domain
 
static const char * DNS_Error
 
static char DNS_FQN [256]
 

Function Documentation

◆ getMyFQN()

static bool getMyFQN ( )
static

Definition at line 51 of file XrdNetIdentity.cc.

52 {
53 // Initialize name, domain, and error to empty strings
54 //
55  memset(DNS_FQN, '\0', sizeof(DNS_FQN));
57  DNS_Error = nullptr;
58 
59 // The identity can be specified via an envar. In this case, it short circuits
60 // all the subsequent code.
61 //
62  const char *dnsName = nullptr;
63  if ((dnsName = getenv("XRDNET_IDENTITY")))
64  {strlcpy(DNS_FQN, dnsName, sizeof(DNS_FQN));
66  if (XrdNetAddrInfo::isHostName(DNS_FQN) && !(DNS_Domain = index(DNS_FQN, '.')))
67  DNS_Domain = "";
68  return false;
69  }
70 
71 // Obtain the host name, this is mandatory.
72 //
73  if (gethostname(DNS_FQN, sizeof(DNS_FQN)))
74  {DNS_Error = XrdSysE2T(errno); DNS_Domain = ""; return false;}
75 
76  int hnLen = strlen(DNS_FQN);
78 
79 // First step it to get all IP addresses configured on this machine
80 //
81  XrdOucTList *ifList = nullptr;
82  if (!XrdNetIF::GetIF(&ifList, &DNS_Error))
83  {DNS_Domain = ""; return true;}
84 
85 // Run through the interfaces and try to get the hostname associated with
86 // this machine. Note that we may have public and private addresses and
87 // they may have different hostname attached. We only accept the hostname
88 // that matches what is returned by gethostname().
89 //
90  XrdNetAddr theAddr;
91  const char *domP = nullptr;
92  char *theIPA[2] = { nullptr, nullptr };
93  char *theName[2] = { nullptr, nullptr };
94  XrdOucTList *ifNow = nullptr;
95 
96  while((ifNow = ifList))
97  {int i = (ifNow->sval[1] ? 1 : 0); // Private | public
98 
99  if (i >= 0 && theName[i] == 0 && !theAddr.Set(ifNow->text, 0)
100  && (dnsName = theAddr.Name(0,&DNS_Error)) && (domP = index(dnsName,'.')))
101  {int n = domP - dnsName;
102  if (n == hnLen && !strncmp(DNS_FQN, dnsName, n))
103  {theName[i] = strdup(dnsName);
104  } else {
105  if (theIPA[i]) free(theIPA[i]);
106  theIPA[i] = strdup(ifNow->text);
107  }
108  }
109  ifList = ifList->next;
110  delete ifNow;
111  }
112 
113 // Fix up error pointer
114 //
115  if (DNS_Error == 0) DNS_Error = "no error";
116 
117 // We prefer the public name should we have it
118 //
119  if (theName[0])
120  {strlcpy(DNS_FQN, theName[0], sizeof(DNS_FQN));
121  goto done;
122  }
123 
124 // Use the private name should we have it
125 //
126  if (theName[1])
127  {strlcpy(DNS_FQN, theName[1], sizeof(DNS_FQN));
128  goto done;
129  }
130 
131 // Concote a name using old-style DNS resolution. This may not work if DNS
132 // namespaces are being used (e.g. k8s environments) or if the hostname is not
133 // resolvable. We will catch that here and move on.
134 //
135  if ((DNS_Error = theAddr.Set(DNS_FQN,0))) dnsName = nullptr;
136  else dnsName = theAddr.Name(0, &DNS_Error);
137 
138 // Check if this worked
139 //
140  if (dnsName)
141  {strlcpy(DNS_FQN, dnsName, sizeof(DNS_FQN));
142  goto done;
143  }
144 
145 // Prefrentially return the hostname as an address as the value of gethostname()
146 // may actually fail. So, we defer naming the machine until later but we do
147 // know its IP address and that can be used as an identity. Return the public
148 // address first. Note that we prefrentially return the IPv6 address here.
149 //
150  if (theIPA[0])
151  {strlcpy(DNS_FQN, theIPA[0], sizeof(DNS_FQN));
152  goto done;
153  }
154 
155  if (theIPA[1])
156  {strlcpy(DNS_FQN, theIPA[1], sizeof(DNS_FQN));
157  goto done;
158  }
159 
160 // Fallback to using the simple unqualified hostname, this still may be OK but
161 // this is likely to fail in certain situations where DNS is screwed up.
162 //
163  DNS_Domain = "";
164  return true;
165 
166 done:
167  free(theName[0]);
168  free(theName[1]);
169  free(theIPA[0]);
170  free(theIPA[1]);
171 
172  if (!(DNS_Domain = index(DNS_FQN, '.')))
173  DNS_Domain = "";
174  return true;
175 }
static const char * DNS_Error
static const char * DNS_Domain
static char DNS_FQN[256]
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:101
size_t strlcpy(char *dst, const char *src, size_t sz)
static bool isHostName(const char *name)
const char * Name(const char *eName=0, const char **eText=0)
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216
static int GetIF(XrdOucTList **ifList, const char **eText=0)
Definition: XrdNetIF.cc:429
XrdOucTList * next
Definition: XrdOucTList.hh:45
char * text
Definition: XrdOucTList.hh:46
static void toLower(char *str)

References DNS_Domain, DNS_Error, DNS_FQN, XrdNetIF::GetIF(), XrdNetAddrInfo::isHostName(), XrdNetAddrInfo::Name(), XrdOucTList::next, XrdNetAddr::Set(), strlcpy(), XrdOucTList::text, XrdOucUtils::toLower(), and XrdSysE2T().

Referenced by initIdentity().

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

◆ initIdentity()

static void initIdentity ( )
inlinestatic

Definition at line 180 of file XrdNetIdentity.cc.

181 {
182  static const bool done = getMyFQN();
183  (void)done;
184 }
static bool getMyFQN()

References getMyFQN().

Referenced by XrdNetIdentity::Domain(), XrdNetIdentity::FQN(), and XrdNetIdentity::SetFQN().

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

Variable Documentation

◆ DNS_Domain

const char* DNS_Domain
static

Definition at line 48 of file XrdNetIdentity.cc.

Referenced by XrdNetIdentity::Domain(), getMyFQN(), and XrdNetIdentity::SetFQN().

◆ DNS_Error

const char* DNS_Error
static

Definition at line 49 of file XrdNetIdentity.cc.

Referenced by XrdNetIdentity::Domain(), XrdNetIdentity::FQN(), and getMyFQN().

◆ DNS_FQN

char DNS_FQN[256]
static

Definition at line 47 of file XrdNetIdentity.cc.

Referenced by XrdNetIdentity::FQN(), getMyFQN(), and XrdNetIdentity::SetFQN().