# SecurityServer.cc

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2002-2005  ONERA
    00004 //
    00005 // This file is part of CERTI-libCERTI
    00006 //
    00007 // CERTI-libCERTI is free software ; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Lesser General Public License
    00009 // as published by the Free Software Foundation ; either version 2 of
    00010 // the License, or (at your option) any later version.
    00011 //
    00012 // CERTI-libCERTI is distributed in the hope that it will be useful, but
    00013 // WITHOUT ANY WARRANTY ; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00015 // Lesser General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Lesser General Public
    00018 // License along with this program ; if not, write to the Free Software
    00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    00020 // USA
    00021 //
    00022 // $Id: SecurityServer.cc,v 3.15 2008/10/13 10:06:48 erk Exp $
    00023 // ----------------------------------------------------------------------------
    00024
    00025
    00026
    00027 #include "SecurityServer.hh"
    00028 #include "PrettyDebug.hh"
    00029
    00030 #include <cassert>
    00031
    00032 using std::list ;
    00033 using std::endl ;
    00034
    00035 static pdCDebug D("SECURITY_SERVER", __FILE__);
    00036 static PrettyDebug G("GENDOC",__FILE__);
    00037
    00038 namespace certi {
    00039
    00040 // ----------------------------------------------------------------------------
    00044 Socket*
    00045 SecurityServer::getSocketLink(FederateHandle theFederate,
    00046                               TransportType theType) const
    00047 {
    00048 // G.Out(pdGendoc,"into SecurityServer::getSocketLink");
    00049 Socket * sock = RTIG_SocketServer.getSocketLink(myFederation, theFederate, theType);
    00050 if ( sock == NULL )
    00051    // Federate killed
    00052    return NULL ;
    00053 else
    00054    return sock ;
    00055 }
    00056
    00057 // ----------------------------------------------------------------------------
    00059 bool
    00060 SecurityServer::dominates(SecurityLevelID A, SecurityLevelID B) const
    00061 {
    00062     return A == B || B == PublicLevelID ;
    00063 }
    00064
    00065 // ----------------------------------------------------------------------------
    00072 bool
    00073 SecurityServer::canFederateAccessData(FederateHandle theFederate,
    00074                                       SecurityLevelID theDataLevelID)
    00075 {
    00076     Socket          *FederateSocket=NULL;
    00077     SecureTCPSocket *SecureSocket=NULL;
    00078
    00079     const char *FederateName ;
    00080     SecurityLevelID FederateLevel ;
    00081
    00082     // 1- Get the socket of this federate
    00083     //(Killed federate/link will throw an RTIinternalError)
    00084     try {
    00085         FederateSocket = getSocketLink(theFederate);
    00086     }
    00087     catch (RTIinternalError &e) {
    00088         return dominates(PublicLevelID, theDataLevelID);
    00089     }
    00090
    00091     // 2- Check if it is a secure socket
    00092     if (NULL!=FederateSocket) {
    00093         SecureSocket = dynamic_cast<SecureTCPSocket *>(FederateSocket);
    00094     }
    00095
    00096     if (NULL==SecureSocket) {
    00097         // If not, all federate are at Public Level.
    00098         return dominates(PublicLevelID, theDataLevelID);
    00099     }
    00100
    00101     // 3- If yes, retrieve federate principal name.
    00102     FederateName = SecureSocket->getPeerName();
    00103
    00104     // 4- Retrieve Federate level
    00105     FederateLevel = getLevel(FederateName);
    00106
    00107     return dominates(FederateLevel, theDataLevelID);
    00108 }
    00109
    00110 // ----------------------------------------------------------------------------
    00112 SecurityServer::SecurityServer(SocketServer &theRTIGServer,
    00113                                AuditFile &theAuditServer,
    00114                                Handle theFederation)
    00115     :  list<SecurityLevel *>(), audit(theAuditServer),
    00116        RTIG_SocketServer(theRTIGServer)
    00117 {
    00118     myFederation = theFederation ;
    00119
    00120     if (myFederation == 0)
    00121         throw RTIinternalError("");
    00122 }
    00123
    00124 // ----------------------------------------------------------------------------
    00126 SecurityServer::~SecurityServer()
    00127 {
    00128     while (!empty()) {
    00129         delete front();
    00130         pop_front();
    00131     }
    00132 }
    00133
    00134 // ----------------------------------------------------------------------------
    00136 SecurityLevelID
    00137 SecurityServer::getLevel(const char *theFederate) const
    00138 {
    00139     return FedLevelList.getLevel(theFederate);
    00140 }
    00141
    00142 // ----------------------------------------------------------------------------
    00144 SecurityLevelID
    00145 SecurityServer::getLevelIDWithName(const char *theName)
    00146 {
    00147     if (empty()) {
    00148     D[pdDebug] << "Empty list: added default (public) level" << endl ;
    00149         insertPublicLevel();
    00150     }
    00151
    00152     if ((theName == NULL) || (strlen(theName) > MAX_SECURITYLEVELNAME)) {
    00153     D[pdDebug] << "Security Level Name null or too long." << endl ;
    00154         throw RTIinternalError("Security Level Name null or too long.");
    00155     }
    00156
    00157     list<SecurityLevel *>::const_iterator i = begin();
    00158     for (; i != end(); i++) {
    00159         if (strcmp((*i)->Name, theName) == 0)
    00160             return (*i)->LevelID ;
    00161     }
    00162
    00163     // Level not Found
    00164     D[pdDebug] << "Level " << theName << " not found. Adding it to the list."
    00165            << endl ;
    00166     LastLevelID ++ ;
    00167     SecurityLevel *StoredLevel = new SecurityLevel(theName, LastLevelID);
    00168     push_back(StoredLevel);
    00169
    00170     return LastLevelID ;
    00171 }
    00172
    00173 // ----------------------------------------------------------------------------
    00175 void
    00176 SecurityServer::insertPublicLevel()
    00177 {
    00178     SecurityLevel *PublicLevel ;
    00179
    00180     PublicLevel = new SecurityLevel(PublicLevelName, PublicLevelID);
    00181     assert(PublicLevel != NULL);
    00182
    00183     push_front(PublicLevel);
    00184
    00185     LastLevelID = PublicLevelID ;
    00186 }
    00187
    00188 // ----------------------------------------------------------------------------
    00190 void
    00191 SecurityServer::registerFederate(const char *the_federate,
    00192                                  SecurityLevelID the_level_id)
    00193 {
    00194     FedLevelList.addFederate(the_federate, the_level_id);
    00195 }
    00196
    00197 }
    00198
    00199 // $Id: SecurityServer.cc,v 3.15 2008/10/13 10:06:48 erk Exp $