AuthenticationManager

XMLAuthenticationFileContent

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
#include "CondCore/CondDB/interface/Auth.h"
#include "CondCore/CondDB/interface/Exception.h"
#include "CondCore/CondDB/interface/CredentialStore.h"
#include "CondCore/Utilities/interface/Utilities.h"
//
#include "RelationalAccess/AuthenticationCredentials.h"
#include "Utilities/Xerces/interface/Xerces.h"
//
#include <iostream>
#include <fstream>
#include <iomanip>

namespace coral_bridge {
  bool parseXMLAuthenticationFile(const std::string& inputFileName, coral_bridge::AuthenticationCredentialSet& data);

  // class for creating the XML Authentication file
  class XMLAuthenticationFileContent {
  public:
    explicit XMLAuthenticationFileContent(std::ostream& out);

    bool openConnectionEntry(const std::string& pcs);

    bool closeConnectionEntry();

    bool openRoleEntry(const std::string& roleName);

    bool closeRoleEntry();

    bool addCredentialEntry(const std::string& userName, const std::string& password);

    void close();

  private:
    std::ostream& m_out;

    bool m_connectionListOpen;

    bool m_connectionEntryOpen;

    bool m_roleEntryOpen;

    unsigned int m_ind;
  };

}  // namespace coral_bridge

namespace cond {
  class AuthenticationManager : public Utilities {
  public:
    AuthenticationManager();
    ~AuthenticationManager() override;
    int execute() override;
  };
}  // namespace cond

cond::AuthenticationManager::AuthenticationManager() : Utilities("cmscond_authentication_manager") {
  addOption<std::string>("authPath", "P", "authentication path");
  addOption<bool>("create", "", "[-s a0 -u a1] create creds db");
  addOption<bool>("reset_admin", "", "[-s a0 -u a1] reset write credentials for admin");
  addOption<bool>("drop", "", "[-c a0 -u a1] drop creds db");
  addOption<bool>("update_princ", "", "[-s a0 -k a1 (-a)] add/update principal");
  addOption<bool>("update_conn", "", "[-s a0 -u a1 (-l a3)] add/update connection");
  addOption<bool>("set_perm", "", "[-s a0 -n a1 -r a2 -c a3 -l a4] set permission");
  addOption<bool>("unset_perm", "", "[-s a0 -n a1 -r a2 -c a3] unset permission");
  addOption<bool>("list_conn", "", "[-s arg ] list connections");
  addOption<bool>("list_perm", "", "[-s a0 (-n a1)(-r a2)(-c a3)] list permissions");
  addOption<bool>("list_princ", "", "[-s arg] list principals");
  addOption<bool>("remove_princ", "", "[-s a0 -n a1] remove principal");
  addOption<bool>("remove_conn", "", "[-s a0 -l a1] remove connection");
  addOption<bool>("admin", "a", "add admin privileges");
  addOption<bool>("force_update", "f", "force update");
  addOption<std::string>("import", "", "[a0 -s a1 -n a2 (-f)] import from the xml file");
  addOption<std::string>("export", "", "[a0 -s a1] export to the xml file");
  addOption<std::string>("service", "s", "service name");
  addOption<std::string>("princ_name", "n", "the principal");
  addOption<std::string>("key", "k", "the key filename");
  addOption<std::string>("role", "r", "the role");
  addOption<std::string>("connectionString", "c", "the connection string");
  addOption<std::string>("connectionLabel", "l", "the connection label");
  addOption<std::string>("userName", "u", "the user name");
}

cond::AuthenticationManager::~AuthenticationManager() {}

int cond::AuthenticationManager::execute() {
  if (hasDebug())
    coral::MessageStream::setMsgVerbosity(coral::Debug);
  std::string authPath("");
  if (hasOptionValue("authPath"))
    authPath = getOptionValue<std::string>("authPath");
  if (authPath.empty()) {
    const char* authEnv = ::getenv(auth::COND_AUTH_PATH);
    if (authEnv) {
      authPath += authEnv;
    } else {
      authEnv = ::getenv("HOME");
      if (authEnv) {
        authPath += authEnv;
      }
    }
  }

  bool drop = hasOptionValue("drop");
  bool create = hasOptionValue("create");
  bool reset_admin = hasOptionValue("reset_admin");
  bool update_princ = hasOptionValue("update_princ");
  bool update_conn = hasOptionValue("update_conn");
  bool list_conn = hasOptionValue("list_conn");
  bool list_perm = hasOptionValue("list_perm");
  bool list_princ = hasOptionValue("list_princ");
  bool remove_princ = hasOptionValue("remove_princ");
  bool remove_conn = hasOptionValue("remove_conn");
  bool set_perm = hasOptionValue("set_perm");
  bool unset_perm = hasOptionValue("unset_perm");
  bool import = hasOptionValue("import");
  bool exp = hasOptionValue("export");

  bool not_exec = true;
  CredentialStore credDb;

  if (drop) {
    std::string connectionString = getOptionValue<std::string>("connectionString");
    std::string userName = getOptionValue<std::string>("userName");
    std::string password = cond::getpassForUser(userName);
    credDb.drop(connectionString, userName, password);
    std::cout << "Credential Repository in " << connectionString << " has been dropped." << std::endl;
    not_exec = false;
  }

  std::string service("");
  if (create && not_exec) {
    service = getOptionValue<std::string>("service");
    std::string userName = getOptionValue<std::string>("userName");
    std::string password = cond::getpassForUser(userName);
    std::string credsStore = credDb.setUpForService(service, authPath);
    credDb.createSchema(credsStore, userName, password);
    std::cout << "Credential Repository for service " << service << " has been created." << std::endl;
    not_exec = false;
  }

  if (reset_admin || update_princ || update_conn || remove_princ || remove_conn || set_perm || unset_perm || import ||
      list_conn || list_princ || list_perm || exp) {
    service = getOptionValue<std::string>("service");
    std::string credsStore = credDb.setUpForService(service, authPath);
    std::cout << "Authenticated principal " << credDb.keyPrincipalName() << std::endl;
    std::cout << "Connecting with credential repository in \"" << credsStore << "\"" << std::endl;
  }

  if (reset_admin && not_exec) {
    std::string userName = getOptionValue<std::string>("userName");
    std::string password = cond::getpassForUser(userName);
    credDb.resetAdmin(userName, password);
    std::cout << "Admin access to service " << credDb.serviceName() << " has been reset." << std::endl;
    not_exec = false;
  }

  if (update_princ && not_exec) {
    bool adminOpt = hasOptionValue("admin");
    std::string key = getOptionValue<std::string>("key");
    auth::DecodingKey pk;
    pk.init(key, auth::COND_KEY);
    credDb.updatePrincipal(pk.principalName(), pk.principalKey(), adminOpt);
    std::cout << "Principal " << pk.principalName() << " has been updated." << std::endl;
    not_exec = false;
  }

  if (update_conn && not_exec) {
    std::string userName = getOptionValue<std::string>("userName");
    std::string password = cond::getpassForUser(userName);
    std::string connectionLabel = schemaLabel(service, userName);
    if (hasOptionValue("connectionLabel")) {
      connectionLabel = getOptionValue<std::string>("connectionLabel");
    }
    credDb.updateConnection(connectionLabel, userName, password);
    std::cout << "Credentials for connection " << connectionLabel << " have been updated." << std::endl;
    not_exec = false;
  }

  if (remove_princ && not_exec) {
    std::string principal = getOptionValue<std::string>("princ_name");
    credDb.removePrincipal(principal);
    std::cout << "Principal " << principal << " has been removed." << std::endl;
    not_exec = false;
  }

  if (remove_conn && not_exec) {
    std::string connectionLabel = getOptionValue<std::string>("connectionLabel");
    credDb.removeConnection(connectionLabel);
    std::cout << "Connection " << connectionLabel << " has been removed." << std::endl;
    not_exec = false;
  }

  if (set_perm && not_exec) {
    std::string principal = getOptionValue<std::string>("princ_name");
    std::string role = getOptionValue<std::string>("role");
    std::string connectionString = getOptionValue<std::string>("connectionString");
    std::string connectionLabel = getOptionValue<std::string>("connectionLabel");
    credDb.setPermission(principal, role, connectionString, connectionLabel);
    std::cout << "Permission for principal " << principal << " to access resource " << connectionString << " with role "
              << role << " has been set." << std::endl;
    not_exec = false;
  }

  if (unset_perm && not_exec) {
    std::string connectionString = getOptionValue<std::string>("connectionString");
    std::string principal("");
    if (hasOptionValue("princ_name"))
      principal = getOptionValue<std::string>("princ_name");
    std::string role("");
    if (hasOptionValue("role"))
      role = getOptionValue<std::string>("role");
    size_t n = credDb.unsetPermission(principal, role, connectionString);
    if (n) {
      std::cout << n << " permissions to access resource " << connectionString;
      if (!role.empty())
        std::cout << " with role " << role;
      std::cout << " have been unset";
      if (!principal.empty())
        std::cout << " for principal " << principal << " ";
    } else {
      std::cout << "No Permission found to access resource " << connectionString;
      if (!role.empty())
        std::cout << " with role " << role;
      if (!principal.empty())
        std::cout << " for principal " << principal << " ";
    }
    std::cout << "." << std::endl;
    not_exec = false;
  }

  if (import && not_exec) {
    bool forceUp = hasOptionValue("force_update");
    std::string fileName = getOptionValue<std::string>("import");
    std::string principal = getOptionValue<std::string>("princ_name");
    coral_bridge::AuthenticationCredentialSet source;
    if (!coral_bridge::parseXMLAuthenticationFile(fileName, source)) {
      std::cout << "Error: XML parsing failed." << std::endl;
      return 1;
    }
    std::cout << "Importing " << source.data().size() << " connection items." << std::endl;
    credDb.importForPrincipal(principal, source, forceUp);
    std::cout << "Credential set for principal " << principal << " has been imported from file " << fileName
              << std::endl;
    not_exec = false;
  }

  if (list_conn && not_exec) {
    std::map<std::string, std::pair<std::string, std::string> > data;
    credDb.listConnections(data);
    std::cout << "Found " << data.size() << " connection(s)." << std::endl;
    std::cout << std::endl;
    static std::string connectionLabelH("connection label");
    static std::string userNameH("username");
    static std::string passwordH("password");
    size_t connectionLabelW = connectionLabelH.size();
    size_t userNameW = 0;
    size_t passwordW = 0;
    for (std::map<std::string, std::pair<std::string, std::string> >::const_iterator iC = data.begin();
         iC != data.end();
         ++iC) {
      const std::string& userName = iC->second.first;
      const std::string& password = iC->second.second;
      const std::string& connectionLabel = iC->first;
      if (connectionLabelW < connectionLabel.size())
        connectionLabelW = connectionLabel.size();
      if (userNameW < userName.size())
        userNameW = userName.size();
      if (passwordW < password.size())
        passwordW = password.size();
    }
    if (userNameW > 0 && userNameW < userNameH.size())
      userNameW = userNameH.size();
    if (passwordW > 0 && passwordW < passwordH.size())
      passwordW = passwordH.size();
    std::cout << std::setiosflags(std::ios_base::left);
    std::cout << std::setw(connectionLabelW) << connectionLabelH;
    if (userNameW) {
      std::cout << "  " << std::setw(userNameW) << userNameH;
    }
    if (passwordW) {
      std::cout << "  " << std::setw(passwordW) << passwordH;
    }
    std::cout << std::endl;
    std::cout << std::setfill('-');
    std::cout << std::setw(connectionLabelW) << "";
    if (userNameW) {
      std::cout << "  " << std::setw(userNameW) << "";
    }
    if (passwordW) {
      std::cout << "  " << std::setw(passwordW) << "";
    }
    std::cout << std::endl;
    std::cout << std::setfill(' ');
    for (std::map<std::string, std::pair<std::string, std::string> >::const_iterator iC = data.begin();
         iC != data.end();
         ++iC) {
      const std::string& connectionLabel = iC->first;
      const std::string& userName = iC->second.first;
      const std::string& password = iC->second.second;
      std::cout << std::setw(connectionLabelW) << connectionLabel << "  " << std::setw(userNameW) << userName << "  "
                << std::setw(passwordW) << password << std::endl;
    }
    not_exec = false;
  }

  if (list_princ && not_exec) {
    std::vector<std::string> data;
    credDb.listPrincipals(data);
    std::cout << "Found " << data.size() << " principal(s)." << std::endl;
    std::cout << std::endl;
    static std::string principalH("principal name");
    size_t principalW = principalH.size();
    for (std::vector<std::string>::const_iterator iP = data.begin(); iP != data.end(); ++iP) {
      const std::string& principal = *iP;
      if (principalW < principal.size())
        principalW = principal.size();
    }
    std::cout << std::setiosflags(std::ios_base::left);
    std::cout << std::setw(principalW) << principalH << std::endl;
    std::cout << std::setfill('-');
    std::cout << std::setw(principalW) << "" << std::endl;
    std::cout << std::setfill(' ');
    for (std::vector<std::string>::const_iterator iP = data.begin(); iP != data.end(); ++iP) {
      std::cout << std::setw(principalW) << *iP << std::endl;
    }
    not_exec = false;
  }

  if (list_perm && not_exec) {
    std::vector<CredentialStore::Permission> data;
    std::string pName("");
    std::string role("");
    std::string conn("");
    if (hasOptionValue("connectionString"))
      conn = getOptionValue<std::string>("connectionString");
    if (hasOptionValue("princ_name"))
      pName = getOptionValue<std::string>("princ_name");
    if (hasOptionValue("role"))
      role = getOptionValue<std::string>("role");
    credDb.selectPermissions(pName, role, conn, data);
    std::cout << "Found " << data.size() << " permission(s)." << std::endl;
    std::cout << std::endl;
    static std::string connectionStringH("connection string");
    static std::string principalH("principal name");
    static std::string roleH("role");
    static std::string connectionLabelH("connection label");
    size_t connectionStringW = connectionStringH.size();
    size_t principalW = principalH.size();
    size_t roleW = roleH.size();
    size_t connectionLabelW = connectionLabelH.size();
    for (std::vector<CredentialStore::Permission>::const_iterator iP = data.begin(); iP != data.end(); ++iP) {
      const std::string& connectionString = iP->connectionString;
      if (connectionStringW < connectionString.size())
        connectionStringW = connectionString.size();
      const std::string& principal = iP->principalName;
      if (principalW < principal.size())
        principalW = principal.size();
      const std::string& role = iP->role;
      if (roleW < role.size())
        roleW = role.size();
      const std::string& connectionLabel = iP->connectionLabel;
      if (connectionLabelW < connectionLabel.size())
        connectionLabelW = connectionLabel.size();
    }
    std::cout << std::setiosflags(std::ios_base::left);
    std::cout << std::setw(connectionStringW) << connectionStringH << "  " << std::setw(principalW) << principalH
              << "  ";
    std::cout << std::setw(roleW) << roleH << "  " << std::setw(connectionLabelW) << connectionLabelH << std::endl;
    std::cout << std::setfill('-');
    std::cout << std::setw(connectionStringW) << ""
              << "  " << std::setw(principalW) << ""
              << "  " << std::setw(roleW) << ""
              << "  " << std::setw(connectionLabelW) << "" << std::endl;
    std::cout << std::setfill(' ');
    for (std::vector<CredentialStore::Permission>::const_iterator iP = data.begin(); iP != data.end(); ++iP) {
      std::cout << std::setw(connectionStringW) << iP->connectionString << "  " << std::setw(principalW)
                << iP->principalName << "  ";
      ;
      std::cout << std::setw(roleW) << iP->role << "  " << std::setw(connectionLabelW) << iP->connectionLabel
                << std::endl;
    }
    not_exec = false;
  }

  if (exp && not_exec) {
    std::string fileName = getOptionValue<std::string>("export");
    coral_bridge::AuthenticationCredentialSet data;
    credDb.exportAll(data);
    std::ofstream outFile(fileName);
    coral_bridge::XMLAuthenticationFileContent xmlFile(outFile);
    const std::map<std::pair<std::string, std::string>, coral::AuthenticationCredentials*>& creds = data.data();
    std::set<std::string> connections;
    bool started = false;
    for (std::map<std::pair<std::string, std::string>, coral::AuthenticationCredentials*>::const_iterator iEntry =
             creds.begin();
         iEntry != creds.end();
         iEntry++) {
      const std::string& connectStr = iEntry->first.first;
      std::set<std::string>::iterator iConn = connections.find(connectStr);
      if (iConn == connections.end()) {
        if (started)
          xmlFile.closeConnectionEntry();
        xmlFile.openConnectionEntry(connectStr);
        started = true;
        connections.insert(connectStr);
        std::pair<std::string, std::string> defRoleKey(connectStr, auth::COND_DEFAULT_ROLE);
        std::map<std::pair<std::string, std::string>, coral::AuthenticationCredentials*>::const_iterator iDef =
            creds.find(defRoleKey);
        if (iDef != creds.end()) {
          xmlFile.addCredentialEntry(iDef->second->valueForItem(coral::IAuthenticationCredentials::userItem()),
                                     iDef->second->valueForItem(coral::IAuthenticationCredentials::passwordItem()));
        }
      }
      const std::string& role = iEntry->first.second;
      if (role != auth::COND_DEFAULT_ROLE) {
        xmlFile.openRoleEntry(role);
        xmlFile.addCredentialEntry(iEntry->second->valueForItem(coral::IAuthenticationCredentials::userItem()),
                                   iEntry->second->valueForItem(coral::IAuthenticationCredentials::passwordItem()));
        xmlFile.closeRoleEntry();
      }
    }
    xmlFile.close();
    not_exec = false;
  }

  if (not_exec) {
    std::cout << "ERROR: no command specified." << std::endl;
    return 1;
  }
  if (hasDebug()) {
    std::cout << "LOGS from the Credential Store:" << std::endl;
    std::cout << credDb.log() << std::endl;
  }
  return 0;
}

int main(int argc, char** argv) {
  cond::AuthenticationManager mgr;
  return mgr.run(argc, argv);
}

#include "xercesc/parsers/XercesDOMParser.hpp"
#include "xercesc/dom/DOM.hpp"
#include "xercesc/sax/HandlerBase.hpp"
#include "xercesc/util/XMLString.hpp"
#include "xercesc/util/PlatformUtils.hpp"

#if defined __SUNPRO_CC
#pragma enable_warn
#elif defined _MSC_VER
#pragma warning(pop)
#endif

bool coral_bridge::parseXMLAuthenticationFile(const std::string& inputFileName, AuthenticationCredentialSet& data) {
  try {
    cms::concurrency::xercesInitialize();
  } catch (const xercesc::XMLException& toCatch) {
    char* message = xercesc::XMLString::transcode(toCatch.getMessage());
    //coral::MessageStream log( serviceName );
    //log << coral::Error << message << coral::MessageStream::endmsg;
    xercesc::XMLString::release(&message);
    return false;
  }

  bool result = true;
  try {
    xercesc::XercesDOMParser parser;
    parser.setValidationScheme(xercesc::XercesDOMParser::Val_Always);
    parser.setDoNamespaces(true);

    xercesc::HandlerBase errorHandler;
    parser.setErrorHandler(&errorHandler);

    parser.parse(inputFileName.c_str());

    xercesc::DOMDocument* document = parser.getDocument();

    XMLCh tempStr[20];
    xercesc::XMLString::transcode("connection", tempStr, 19);

    xercesc::DOMNodeList* connectionList = document->getElementsByTagName(tempStr);

    if (connectionList) {
      XMLSize_t numberOfConnections = connectionList->getLength();

      for (XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection) {
        xercesc::DOMNode* connectionNode = connectionList->item(iConnection);

        if (connectionNode) {
          char* connectionName =
              xercesc::XMLString::transcode(connectionNode->getAttributes()->item(0)->getNodeValue());
          std::string sConnectionName = connectionName;
          xercesc::XMLString::release(&connectionName);

          xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();

          if (parameterList) {
            XMLSize_t numberOfParameters = parameterList->getLength();

            for (XMLSize_t iParameter = 0; iParameter < numberOfParameters; ++iParameter) {
              xercesc::DOMNode* parameterNode = parameterList->item(iParameter);

              if (parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
                char* nodeName = xercesc::XMLString::transcode(parameterNode->getNodeName());
                std::string sNodeName = nodeName;
                xercesc::XMLString::release(&nodeName);

                if (sNodeName == "parameter") {  // The default parameters
                  char* parameterName =
                      xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
                  std::string sParameterName = parameterName;
                  xercesc::XMLString::release(&parameterName);
                  char* parameterValue =
                      xercesc::XMLString::transcode(parameterNode->getAttributes()->item(1)->getNodeValue());
                  std::string sParameterValue = parameterValue;
                  xercesc::XMLString::release(&parameterValue);

                  data.registerItem(sConnectionName, sParameterName, sParameterValue);
                } else if (sNodeName == "role") {  // A role
                  char* roleName =
                      xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
                  std::string sRoleName = roleName;
                  xercesc::XMLString::release(&roleName);

                  // Retrieve the parameters for the role
                  xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();

                  if (roleParameterList) {
                    XMLSize_t numberOfRoleParameters = roleParameterList->getLength();

                    for (XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter) {
                      xercesc::DOMNode* roleParameterNode = roleParameterList->item(iRoleParameter);
                      if (roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
                        char* roleNodeName = xercesc::XMLString::transcode(roleParameterNode->getNodeName());
                        std::string sRoleNodeName = roleNodeName;
                        xercesc::XMLString::release(&roleNodeName);

                        if (sRoleNodeName == "parameter") {
                          char* roleParameterName = xercesc::XMLString::transcode(
                              roleParameterNode->getAttributes()->item(0)->getNodeValue());
                          std::string sRoleParameterName = roleParameterName;
                          xercesc::XMLString::release(&roleParameterName);
                          char* roleParameterValue = xercesc::XMLString::transcode(
                              roleParameterNode->getAttributes()->item(1)->getNodeValue());
                          std::string sRoleParameterValue = roleParameterValue;
                          xercesc::XMLString::release(&roleParameterValue);

                          data.registerItem(sConnectionName, sRoleName, sRoleParameterName, sRoleParameterValue);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    parser.reset();
  } catch (const xercesc::XMLException& toCatch) {
    char* message = xercesc::XMLString::transcode(toCatch.getMessage());
    //coral::MessageStream log( serviceName );
    //log << coral::Error << message << coral::MessageStream::endmsg;
    xercesc::XMLString::release(&message);
    result = false;
  } catch (const xercesc::DOMException& toCatch) {
    char* message = xercesc::XMLString::transcode(toCatch.msg);
    //coral::MessageStream log( serviceName );
    //log << coral::Error << message << coral::MessageStream::endmsg;
    xercesc::XMLString::release(&message);
    result = false;
  } catch (...) {
    //coral::MessageStream log( serviceName );
    //log << coral::Error << "Unexpected Exception parsing file \"" << inputFileName << "\"" << coral::MessageStream::endmsg;
    result = false;
  }

  cms::concurrency::xercesTerminate();

  return result;
}

#include "RelationalAccess/IAuthenticationCredentials.h"
#include <iomanip>

coral_bridge::XMLAuthenticationFileContent::XMLAuthenticationFileContent(std::ostream& out)
    : m_out(out), m_connectionListOpen(false), m_connectionEntryOpen(false), m_roleEntryOpen(false), m_ind(0) {
  m_out << "<?xml version=\"1.0\" ?>" << std::endl;
  m_out << "<connectionlist>" << std::endl;
  m_connectionListOpen = true;
}

bool coral_bridge::XMLAuthenticationFileContent::openConnectionEntry(const std::string& pcs) {
  bool ret = false;
  if (m_connectionListOpen && !m_connectionEntryOpen) {
    m_out << std::endl;
    m_ind += 2;
    m_out << std::setw(m_ind) << "";
    m_out << "<connection name=\"" << pcs << "\" >" << std::endl;
    m_connectionEntryOpen = true;
    ret = true;
  }
  return ret;
}

bool coral_bridge::XMLAuthenticationFileContent::closeConnectionEntry() {
  bool ret = false;
  if (m_connectionEntryOpen) {
    m_out << std::setw(m_ind) << "";
    m_out << "</connection>" << std::endl;
    m_ind -= 2;
    ret = true;
    m_connectionEntryOpen = false;
  }
  return ret;
}

bool coral_bridge::XMLAuthenticationFileContent::openRoleEntry(const std::string& roleName) {
  bool ret = false;
  if (m_connectionEntryOpen && !m_roleEntryOpen) {
    m_ind += 2;
    m_out << std::setw(m_ind) << "";
    m_out << "<role name=\"" << roleName << "\" >" << std::endl;
    m_roleEntryOpen = true;
    ret = true;
  }
  return ret;
}

bool coral_bridge::XMLAuthenticationFileContent::closeRoleEntry() {
  bool ret = false;
  if (m_roleEntryOpen) {
    m_out << std::setw(m_ind) << "";
    m_out << "</role>" << std::endl;
    m_ind -= 2;
    ret = true;
    m_roleEntryOpen = false;
  }
  return ret;
}

bool coral_bridge::XMLAuthenticationFileContent::addCredentialEntry(const std::string& userName,
                                                                    const std::string& password) {
  bool ret = false;
  if (m_connectionEntryOpen) {
    m_out << std::setw(m_ind + 2) << "";
    m_out << "<parameter name=\"" << coral::IAuthenticationCredentials::userItem() << "\" value=\"" << userName
          << "\" />" << std::endl;
    m_out << std::setw(m_ind + 2) << "";
    m_out << "<parameter name=\"" << coral::IAuthenticationCredentials::passwordItem() << "\" value=\"" << password
          << "\" />" << std::endl;
    ret = true;
  }
  return ret;
}

void coral_bridge::XMLAuthenticationFileContent::close() {
  if (m_connectionListOpen) {
    if (m_connectionEntryOpen) {
      if (m_roleEntryOpen) {
        closeRoleEntry();
      }
      closeConnectionEntry();
    }
    m_out << std::endl;
    m_out << "</connectionlist>" << std::endl;
  }
  m_connectionListOpen = false;
}