XRootD
XrdLinkMatch.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d L i n k M a t c h . c c */
4 /* */
5 /* (c) 2005 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 <cstring>
31 
32 #include "Xrd/XrdLinkMatch.hh"
33 #include "XrdSys/XrdSysPlatform.hh"
34 
35 /******************************************************************************/
36 /* M a t c h */
37 /******************************************************************************/
38 
39 int XrdLinkMatch::Match(const char *uname, int unlen,
40  const char *hname, int hnlen)
41 {
42 
43 // Check if we should try to match the username
44 //
45  if (Unamelen && (Unamelen > unlen+1 || strncmp(uname,Uname,Unamelen))) return 0;
46 
47 // Check if we should match the full host name
48 //
49  if (HnameL && !HnamelenL) return !strcmp(HnameL, hname);
50 
51 // Check if prefix suffix matching might succeed
52 //
53  if (HnamelenL > hnlen) return 0;
54 
55 // Check if we should match the host name prefix
56 //
57  if (HnameL && strncmp(HnameL, hname, HnamelenL)) return 0;
58 
59 // Check if we should match the host name suffix
60 //
61  if (!HnameR) return 1;
62  return !strcmp(hname+hnlen-HnamelenR, hname);
63 }
64 
65 /******************************************************************************/
66 /* S e t */
67 /******************************************************************************/
68 
69 void XrdLinkMatch::Set(const char *target)
70 {
71  char *theast;
72 
73 // Free any existing target
74 //
75  if (!target || !strcmp(target, "*"))
76  {Uname = HnameL = HnameR = 0;
77  Unamelen = HnamelenL = HnamelenR = 0;
78  return;
79  }
80  strlcpy(Buff, target, sizeof(Buff)-1);
81  Uname = Buff;
82 
83 // Find the '@' as the pivot in this name
84 //
85  if (!(HnameL = index(Uname, '@')))
86  {if ((Unamelen = strlen(Uname)))
87  {if (Uname[Unamelen-1] == '*') Unamelen--;
88  else if (index(Uname, ':')) Uname[Unamelen++] = '@';
89  else if (index(Uname, '.')) Uname[Unamelen++] = ':';
90  else Uname[Unamelen++] = '.';
91  }
92  HnameR = 0;
93  return;
94  }
95 
96 // We have a form of <string>@<string>
97 //
98  *HnameL++ = '\0';
99  if ((Unamelen = strlen(Uname)))
100  {if (Uname[Unamelen-1] == '*') Unamelen--;
101  else if (index(Uname, ':')) Uname[Unamelen++] = '@';
102  else if (index(Uname, '.')) Uname[Unamelen++] = ':';
103  else Uname[Unamelen++] = '.';
104  }
105 
106 // The post string may have an asterisk.
107 //
108  if (!(theast = index(HnameL, '*')))
109  {HnamelenL = 0;
110  HnameR = 0;
111  return;
112  }
113 
114 // Indicate how much of the prefix should match
115 //
116  *theast = '\0';
117  if (!(HnamelenL = strlen(HnameL))) HnameL = 0;
118 
119 // Indicate how much of the suffix should match
120 //
121  if ((HnamelenR = strlen(theast))) HnameR = theast+1;
122  else HnameR = 0;
123  Hnamelen = HnamelenL+HnamelenR;
124 }
size_t strlcpy(char *dst, const char *src, size_t sz)
void Set(const char *target)
Definition: XrdLinkMatch.cc:69
int Match(const char *uname, int unlen, const char *hname, int hnlen)
Definition: XrdLinkMatch.cc:39