// FileGsiAuthorizer.cxx #include "dial_gsi/FileGsiAuthorizer.h" #include "dataset_util/DtdRegistry.h" #include #include using std::string; using std::map; using std::endl; using std::cout; using std::ostream; using std::ifstream; using std::auto_ptr; using dial::GsiAuthorizer; using dial::FileGsiAuthorizer; //********************************************************************** // Local Definitions. //********************************************************************** namespace { // Read identities/roles from a file into a set void read(string name, map& gsi) { string cert; string userid = ""; ifstream fp(name.c_str()); if ( ! fp ) { cout << "FileGsiAuthorizer: File read error" << endl; } while ( fp ) { string line; getline(fp, line); // empty line if( line.empty() ) continue; // Remove leading and traing whitespace. string::size_type ipos = 0; while ( ipos < line.size() && isspace(line[ipos]) ) ++ipos; string::size_type jpos = line.size(); while ( jpos > 0 && isspace(line[jpos-1]) ) --jpos; string sline = line.substr(ipos, jpos-ipos); // If comment, ignore if ( sline[0] == '#' ) continue; // If first character is double-quote, then use everything up to // the next double quote. if ( sline[0] == '"' ) { string::size_type ipos = sline.find('"', 1); if ( ipos == string::npos ) { cout << "FileGsiAuthorizer: Invalid line:" << endl; cout << line << endl; break; } cert = sline.substr(1, ipos-1); userid = sline.substr(ipos+1, string::npos); // Otherwise take the entire line. } else { cert = sline; userid = ""; } gsi[cert] = userid; } fp.close(); } //********************************************************************** // Creator. GsiAuthorizer* create(const XmlElement& ele) { if ( ele.name() != FileGsiAuthorizer::xml_name() ) return 0; if ( ! ele.has_attribute("fname")) { return 0; } string filename = ele.attribute("fname"); FileGsiAuthorizer* pgsi = new FileGsiAuthorizer(filename); return pgsi; } //********************************************************************** // Register creator. int STAT_FileGsiAuthorizer = GsiAuthorizer:: register_creator(FileGsiAuthorizer::xml_name(), create); //********************************************************************** // Register the DTD. DtdRegistry::Status ISTAT_DTD_FileGsiAuthorizer = DtdRegistry::register_dtd("dial"); //********************************************************************** } // end unnamed namespace //********************************************************************** // Static member functions. //********************************************************************** // DTD // ANY = some kind of job creator const Text& FileGsiAuthorizer::dtd() { static Text txt; if ( txt.size() == 0 ) { txt.append(""); txt.append(""); } return txt; } //********************************************************************** // Member functions. //********************************************************************** // Constructor from a file FileGsiAuthorizer::FileGsiAuthorizer(string fname) : m_fname(fname) { read(m_fname, m_gsi_map); } //********************************************************************** // XML Constructor FileGsiAuthorizer::FileGsiAuthorizer(const XmlElement& ele) { // Check the input if(ele.name() != xml_name()) return; //Check XML for fname if(! ele.has_attribute("fname")) return; // Fetch the attribute and fill the data string filename = ele.attribute("fname"); m_fname = filename; read(m_fname, m_gsi_map); } //********************************************************************** // Return if certificate is valid for a person bool FileGsiAuthorizer::is_authorized(Identity id) const { map::const_iterator it = m_gsi_map.find(id); return it != m_gsi_map.end(); } //********************************************************************** // Return if a person is authorized for given role // Returns false as roles not supported bool FileGsiAuthorizer:: is_authorized_with_role(Identity, Role) const { return false; } //********************************************************************** // Return update status int FileGsiAuthorizer::update() { read(m_fname, m_gsi_map); return 0; } //********************************************************************** // Output stream ostream& FileGsiAuthorizer::ostr(ostream &lhs) const { for( map::const_iterator it = m_gsi_map.begin(); it != m_gsi_map.end(); ++it) lhs << (*it).first << " " << (*it).second << endl; return lhs; } //********************************************************************** // Scheduler const interface //********************************************************************** // XML. const XmlElement* FileGsiAuthorizer::xml() const { auto_ptr pele(new XmlElement(xml_name())); pele->add_attribute("fname", m_fname); return pele.release(); } //**********************************************************************