00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 const char *XrdNetSecurityCVSID = "$Id: XrdNetSecurity.cc 25932 2008-10-23 10:58:11Z ganis $";
00014
00015 #ifndef WIN32
00016 #include <netdb.h>
00017 #include <stdlib.h>
00018 #include <strings.h>
00019 #include <sys/types.h>
00020 #include <sys/socket.h>
00021 #include <netinet/in.h>
00022 #include <arpa/inet.h>
00023 #else
00024 #include <stdlib.h>
00025 #include <sys/types.h>
00026 #include <Winsock2.h>
00027 #include <io.h>
00028 int innetgr(const char *netgroup, const char *host, const char *user,
00029 const char *domain)
00030 {
00031 return 0;
00032 }
00033 #include "XrdSys/XrdWin32.hh"
00034 #endif
00035
00036 #include "XrdNet/XrdNetDNS.hh"
00037 #include "XrdNet/XrdNetSecurity.hh"
00038 #include "XrdOuc/XrdOucTrace.hh"
00039
00040
00041
00042
00043
00044 class XrdNetTextList
00045 {
00046 public:
00047
00048 XrdNetTextList *next;
00049 char *text;
00050
00051 XrdNetTextList(char *newtext) {next = 0; text = strdup(newtext);}
00052 ~XrdNetTextList() {if (text) free(text);}
00053 };
00054
00055
00056
00057
00058
00059 #define DEBUG(x) if (eTrace) {eTrace->Beg(TraceID); cerr <<x; eTrace->End();}
00060
00061
00062
00063
00064
00065 const char *XrdNetSecurity::TraceID = "NetSecurity";
00066
00067
00068
00069
00070
00071 void XrdNetSecurity::AddHost(char *hname)
00072 {
00073 char *Hname = 0;
00074
00075
00076
00077 if (isdigit(*hname) && (Hname = XrdNetDNS::getHostName(hname)))
00078 OKHosts.Add(hname, Hname, 0, Hash_dofree);
00079 else {XrdOucNList *nlp = new XrdOucNList(hname);
00080 HostList.Insert(nlp);
00081 }
00082 if (Hname) {DEBUG(hname <<" (" <<Hname <<") added to authorized hosts.");}
00083 else {DEBUG(hname <<" added to authorized hosts.");}
00084 }
00085
00086
00087
00088
00089
00090 void XrdNetSecurity::AddNetGroup(char *gname)
00091 {
00092 XrdNetTextList *tlp = new XrdNetTextList(gname);
00093
00094
00095
00096 tlp->next = NetGroups;
00097 NetGroups = tlp;
00098
00099
00100
00101 DEBUG(gname <<" added to authorized netgroups.");
00102 }
00103
00104
00105
00106
00107
00108 char *XrdNetSecurity::Authorize(struct sockaddr *addr)
00109 {
00110 struct sockaddr_in *ip = (struct sockaddr_in *)addr;
00111 char ipbuff[64], *hname;
00112 const char *ipname;
00113 XrdNetTextList *tlp;
00114
00115
00116
00117 if (!(ipname = (char *)inet_ntop(ip->sin_family, (void *)&(ip->sin_addr),
00118 ipbuff, sizeof(ipbuff)))) return (char *)0;
00119
00120
00121
00122 okHMutex.Lock();
00123 if ((hname = OKHosts.Find(ipname)))
00124 {okHMutex.UnLock(); return strdup(hname);}
00125
00126
00127
00128 if (!(hname = XrdNetDNS::getHostName(*addr))) hname = strdup(ipname);
00129
00130
00131
00132 if ((tlp = NetGroups))
00133 do {if (innetgr(tlp->text, hname, 0, 0))
00134 return hostOK(hname, ipname, "netgroup");
00135 } while ((tlp = tlp->next));
00136
00137
00138
00139 if (HostList.Find(hname)) return hostOK(hname, ipname, "host");
00140
00141
00142
00143 okHMutex.UnLock();
00144 DEBUG(hname <<" not authorized");
00145 free(hname);
00146 return 0;
00147 }
00148
00149
00150
00151
00152
00153 void XrdNetSecurity::Merge(XrdNetSecurity *srcp)
00154 {
00155 XrdOucNList *np;
00156 XrdNetTextList *sp, *tp;
00157
00158
00159
00160 while((np = srcp->HostList.Pop())) HostList.Replace(np);
00161
00162
00163
00164 while((sp = srcp->NetGroups))
00165 {tp = NetGroups; srcp->NetGroups = sp->next;
00166 while(tp) if (!strcmp(tp->text, sp->text)) break;
00167 else tp = tp->next;
00168 if (tp) delete sp;
00169 else {sp->next = NetGroups;
00170 NetGroups = sp;
00171 }
00172 }
00173
00174
00175
00176 delete srcp;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186 char *XrdNetSecurity::hostOK(char *hname, const char *ipname, const char *why)
00187 {
00188
00189
00190
00191
00192 OKHosts.Add(ipname, strdup(hname), lifetime, Hash_dofree);
00193 okHMutex.UnLock();
00194 DEBUG(hname <<" authorized via " <<why);
00195 return hname;
00196 }