XRootD
XrdNetIdentity.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d N e t I d e n t i t y . h h */
4 /* */
5 /* (c) 2021 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "XrdNet/XrdNetAddr.hh"
34 #include "XrdNet/XrdNetIdentity.hh"
35 #include "XrdNet/XrdNetIF.hh"
36 #include "XrdOuc/XrdOucTList.hh"
37 #include "XrdSys/XrdSysE2T.hh"
38 
39 /******************************************************************************/
40 /* O n e T i m e S t a t i c I n i t i a l i z a t i o n */
41 /******************************************************************************/
42 
43 namespace
44 {
45 char *getMyFQN(const char *&myDom, const char *&myErr)
46 {
47  XrdNetAddr theAddr;
48  XrdOucTList *ifList, *ifNow;
49  const char *dnsName, *domP;
50  char *theName[2] = {0}, *theDom[2] = {0}, hName[256];
51  int hnLen;
52 
53 // Obtain the host name, this is mandatory.
54 //
55  if (gethostname(hName, sizeof(hName)))
56  {myErr = XrdSysE2T(errno); myDom = 0; return 0;}
57  hnLen = strlen(hName);
58 
59 // First step it to get all IP addresses configured on this machine
60 //
61  if (!XrdNetIF::GetIF(&ifList, &myErr))
62  {myDom = 0; return strdup(hName);}
63 
64 // Run through the interfaces and try to get the hostname associated with
65 // this machine. Note that we may have public and private addresses and
66 // they may have different hostname attached. We only accept the hostname
67 // that matches what is returned by gethostname().
68 //
69  while((ifNow = ifList))
70  {int i = (ifNow->sval[1] ? 1 : 0); // Private | public
71 
72  if (i >= 0 && theName[i] == 0 && !theAddr.Set(ifNow->text, 0)
73  && (dnsName = theAddr.Name(0,&myErr)) && (domP = index(dnsName,'.')))
74  {int n = domP - dnsName;
75  if (n == hnLen && !strncmp(hName, dnsName, n))
76  {theName[i] = strdup(dnsName);
77  theDom[i] = theName[i] + n;
78  }
79  }
80  ifList = ifList->next;
81  delete ifNow;
82  }
83 
84 // Fix up error pointer
85 //
86  if (myErr == 0) myErr = "no error";
87 
88 // We prefer the public name should we have it
89 //
90  if (theName[0])
91  {if (theName[1]) free(theName[1]);
92  myDom = theDom[0];
93  return theName[0];
94  }
95 
96 // Use the private name should we have it
97 //
98  if (theName[1])
99  {myDom = theDom[1];
100  return theName[1];
101  }
102 
103 // Concote a name using old-style DNS resolution. This may not work if
104 // DNS namespaces are being used (e.g. k8s environments).
105 //
106  if ((myErr = theAddr.Set(hName,0))) dnsName = 0;
107  else dnsName = theAddr.Name(0, &myErr);
108 
109 // Check if this worked
110 //
111  if (dnsName)
112  {theName[0] = strdup(dnsName);
113  if (!(myDom = index(theName[0], '.'))) myDom = "";
114  return theName[0];
115  }
116 
117 // Fallback to using the simple unqualified hostname, this still mae OK
118 //
119  theName[0] = strdup(hName);
120  myDom = theName[0] + hnLen;
121  return theName[0];
122 }
123 }
124 
125 /******************************************************************************/
126 /* S t a t i c M e m b e r s */
127 /******************************************************************************/
128 
129 // Note that we are gauranteed that this will be fully initialzed prior
130 // to any method called that uses these values irrespective of static
131 // initialization order, even though statically initialized.
132 
133 const char *XrdNetIdentity::DNS_Domain;
134 const char *XrdNetIdentity::DNS_Error;
135 const char *XrdNetIdentity::DNS_FQN = getMyFQN(DNS_Domain, DNS_Error);
136 
137 /******************************************************************************/
138 /* D o m a i n */
139 /******************************************************************************/
140 
141 const char *XrdNetIdentity::Domain(const char **eText)
142 {
143  if (eText) *eText = DNS_Error;
144  return DNS_Domain;
145 }
146 
147 /******************************************************************************/
148 /* F Q N */
149 /******************************************************************************/
150 
151 const char *XrdNetIdentity::FQN(const char **eText)
152 {
153  if (eText) *eText = DNS_Error;
154  return DNS_FQN;
155 }
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:99
const char * Name(const char *eName=0, const char **eText=0)
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:208
static int GetIF(XrdOucTList **ifList, const char **eText=0)
Definition: XrdNetIF.cc:413
static const char * Domain(const char **eText=0)
static const char * FQN(const char **etext=0)
XrdOucTList * next
Definition: XrdOucTList.hh:45
char * text
Definition: XrdOucTList.hh:46