ldif.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       ldif.cc
00003 ///             Routines for reading and writing LDAP LDIF data.
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "ldif.h"
00023 #include "record.h"
00024 #include "r_contact.h"
00025 #include "base64.h"
00026 #include <stdexcept>
00027 #include <iostream>
00028 #include <iomanip>
00029 #include <string.h>
00030 
00031 #define __DEBUG_MODE__
00032 #include "debug.h"
00033 
00034 namespace Barry {
00035 
00036 const ContactLdif::NameToFunc ContactLdif::FieldMap[] = {
00037         { "Email", "Email address",
00038                 &ContactLdif::Email, &ContactLdif::SetEmail },
00039         { "Phone", "Phone number",
00040                 &ContactLdif::Phone, &ContactLdif::SetPhone },
00041         { "Fax", "Fax number",
00042                 &ContactLdif::Fax, &ContactLdif::SetFax },
00043         { "WorkPhone", "Work phone number",
00044                 &ContactLdif::WorkPhone, &ContactLdif::SetWorkPhone },
00045         { "HomePhone", "Home phone number",
00046                 &ContactLdif::HomePhone, &ContactLdif::SetHomePhone },
00047         { "MobilePhone", "Mobile phone number",
00048                 &ContactLdif::MobilePhone, &ContactLdif::SetMobilePhone },
00049         { "Pager", "Pager number",
00050                 &ContactLdif::Pager, &ContactLdif::SetPager },
00051         { "PIN", "PIN",
00052                 &ContactLdif::PIN, &ContactLdif::SetPIN },
00053         { "FirstName", "First name",
00054                 &ContactLdif::FirstName, &ContactLdif::SetFirstName },
00055         { "LastName", "Last name",
00056                 &ContactLdif::LastName, &ContactLdif::SetLastName },
00057         { "Company", "Company name",
00058                 &ContactLdif::Company, &ContactLdif::SetCompany },
00059         { "DefaultCommunicationsMethod", "Default communications method",
00060                 &ContactLdif::DefaultCommunicationsMethod, &ContactLdif::SetDefaultCommunicationsMethod },
00061         { "WorkAddress1", "Work Address, line 1",
00062                 &ContactLdif::WorkAddress1, &ContactLdif::SetWorkAddress1 },
00063         { "WorkAddress2", "Work Address, line 2",
00064                 &ContactLdif::WorkAddress2, &ContactLdif::SetWorkAddress2 },
00065         { "WorkAddress3", "Work Address, line 3",
00066                 &ContactLdif::WorkAddress3, &ContactLdif::SetWorkAddress3 },
00067         { "WorkCity", "WorkCity",
00068                 &ContactLdif::WorkCity, &ContactLdif::SetWorkCity },
00069         { "WorkProvince", "WorkProvince / State",
00070                 &ContactLdif::WorkProvince, &ContactLdif::SetWorkProvince },
00071         { "WorkPostalCode", "Work Postal / ZIP code",
00072                 &ContactLdif::WorkPostalCode, &ContactLdif::SetWorkPostalCode },
00073         { "WorkCountry", "WorkCountry",
00074                 &ContactLdif::WorkCountry, &ContactLdif::SetWorkCountry },
00075         { "JobTitle", "Job Title",
00076                 &ContactLdif::JobTitle, &ContactLdif::SetJobTitle },
00077         { "PublicKey", "Public key",
00078                 &ContactLdif::PublicKey, &ContactLdif::SetPublicKey },
00079         { "Notes", "Notes",
00080                 &ContactLdif::Notes, &ContactLdif::SetNotes },
00081         { "WorkPostalAddress", "Mailing Work address (includes address lines, city, province, country, and postal code)",
00082                 &ContactLdif::WorkPostalAddress, &ContactLdif::SetWorkPostalAddress },
00083         { "HomePostalAddress", "Mailing home address (includes address lines, city, province, country, and postal code)",
00084                 &ContactLdif::HomePostalAddress, &ContactLdif::SetHomePostalAddress },
00085         { "FullName", "First + Last names",
00086                 &ContactLdif::FullName, &ContactLdif::SetFullName },
00087         { "FQDN", "Fully qualified domain name",
00088                 &ContactLdif::FQDN, &ContactLdif::SetFQDN },
00089         { 0, 0, 0 }
00090 };
00091 
00092 
00093 bool ContactLdif::LdifAttribute::operator<(const LdifAttribute &other) const
00094 {
00095         // the dn attribute always comes first in LDIF output
00096         if( name == "dn" ) {
00097                 if( other.name == "dn" )
00098                         return false;   // both dn, so equal
00099                 return true;
00100         }
00101         else if( other.name == "dn" )
00102                 return false;
00103 
00104         return (order < other.order && name != other.name) ||
00105                 (order == other.order && name < other.name);
00106 }
00107 
00108 bool ContactLdif::LdifAttribute::operator==(const LdifAttribute &other) const
00109 {
00110         return name == other.name;
00111 }
00112 
00113 
00114 ///////////////////////////////////////////////////////////////////////////////
00115 // ContactLdif class
00116 
00117 ContactLdif::ContactLdif(const std::string &baseDN)
00118         : m_baseDN(baseDN)
00119 {
00120         // setup some sane defaults
00121         Map("mail", &ContactLdif::Email, &ContactLdif::SetEmail);
00122         Map("facsimileTelephoneNumber", &ContactLdif::Fax, &ContactLdif::SetFax);
00123         Map("telephoneNumber", &ContactLdif::WorkPhone, &ContactLdif::SetWorkPhone);
00124         Map("homePhone", &ContactLdif::HomePhone, &ContactLdif::SetHomePhone);
00125         Map("mobile", &ContactLdif::MobilePhone, &ContactLdif::SetMobilePhone);
00126         Map("pager", &ContactLdif::Pager, &ContactLdif::SetPager);
00127         Map("l", &ContactLdif::WorkCity, &ContactLdif::SetWorkCity);
00128         Map("st", &ContactLdif::WorkProvince, &ContactLdif::SetWorkProvince);
00129         Map("postalCode", &ContactLdif::WorkPostalCode, &ContactLdif::SetWorkPostalCode);
00130         Map("o", &ContactLdif::Company, &ContactLdif::SetCompany);
00131         Map("c", &ContactLdif::WorkCountry, &ContactLdif::SetWorkCountry);
00132         SetObjectClass("c", "country");
00133 
00134         Map("title", &ContactLdif::JobTitle, &ContactLdif::SetJobTitle);
00135         Map("dn", &ContactLdif::FQDN, &ContactLdif::SetFQDN);
00136         Map("displayName", &ContactLdif::FullName, &ContactLdif::SetFullName);
00137         Map("cn", &ContactLdif::FullName, &ContactLdif::SetFullName);
00138         Map("sn", &ContactLdif::LastName, &ContactLdif::SetLastName);
00139         Map("givenName", &ContactLdif::FirstName, &ContactLdif::SetFirstName);
00140         Map("street", &ContactLdif::WorkAddress1, &ContactLdif::SetWorkAddress1);
00141         Map("postalAddress", &ContactLdif::WorkPostalAddress, &ContactLdif::SetWorkPostalAddress);
00142         Map("homePostalAddress", &ContactLdif::HomePostalAddress, &ContactLdif::SetHomePostalAddress);
00143         Map("note", &ContactLdif::Notes, &ContactLdif::SetNotes);
00144 
00145         // add heuristics hooks
00146         Hook("cn", &m_cn);
00147         Hook("displayName", &m_displayName);
00148         Hook("sn", &m_sn);
00149         Hook("givenName", &m_givenName);
00150 
00151         // set default DN attribute
00152         SetDNAttr("cn");
00153 }
00154 
00155 ContactLdif::~ContactLdif()
00156 {
00157 }
00158 
00159 void ContactLdif::DoWrite(Barry::Contact &con,
00160                           const std::string &attr,
00161                           const std::string &data)
00162 {
00163         // valid?
00164         if( attr.size() == 0 || data.size() == 0 )
00165                 return;
00166 
00167         // now have attr/data pair, check hooks:
00168         HookMapType::iterator hook = m_hookMap.find(attr);
00169         if( hook != m_hookMap.end() ) {
00170                 *(hook->second) = data;
00171         }
00172 
00173         // run according to map
00174         AccessMapType::iterator acc = m_map.find(attr);
00175         if( acc != m_map.end() ) {
00176                 (this->*(acc->second.write))(con, data);
00177         }
00178 }
00179 
00180 void ContactLdif::Hook(const std::string &ldifname, std::string *var)
00181 {
00182         m_hookMap[ldifname] = var;
00183 }
00184 
00185 const ContactLdif::NameToFunc*
00186 ContactLdif::GetField(const std::string &fieldname) const
00187 {
00188         for( const NameToFunc *n = FieldMap; n->name; n++ ) {
00189                 if( fieldname == n->name )
00190                         return n;
00191         }
00192         return 0;
00193 }
00194 
00195 std::string ContactLdif::GetFieldReadName(GetFunctionType read) const
00196 {
00197         for( const NameToFunc *n = FieldMap; n->name; n++ ) {
00198                 if( read == n->read )
00199                         return n->name;
00200         }
00201         return "<unknown>";
00202 }
00203 
00204 std::string ContactLdif::GetFieldWriteName(SetFunctionType write) const
00205 {
00206         for( const NameToFunc *n = FieldMap; n->name; n++ ) {
00207                 if( write == n->write )
00208                         return n->name;
00209         }
00210         return "<unknown>";
00211 }
00212 
00213 bool ContactLdif::Map(const LdifAttribute &ldifname,
00214                       const std::string &readField,
00215                       const std::string &writeField)
00216 {
00217         const NameToFunc *read = GetField(readField);
00218         const NameToFunc *write = GetField(writeField);
00219         if( !read || !write )
00220                 return false;
00221         Map(ldifname, read->read, write->write);
00222         return true;
00223 }
00224 
00225 void ContactLdif::Map(const LdifAttribute &ldifname,
00226                       GetFunctionType read,
00227                       SetFunctionType write)
00228 {
00229         m_map[ldifname] = AccessPair(read, write);
00230 }
00231 
00232 void ContactLdif::Unmap(const LdifAttribute &ldifname)
00233 {
00234         m_map.erase(ldifname);
00235 }
00236 
00237 //
00238 // SetDNAttr
00239 //
00240 /// Sets the LDIF attribute name to use when constructing the FQDN.
00241 /// The FQDN field will take this name, and combine it with the
00242 /// baseDN from the constructor to produce a FQDN for the record.
00243 ///
00244 bool ContactLdif::SetDNAttr(const LdifAttribute &name)
00245 {
00246         // try to find the attribute in the map
00247         AccessMapType::iterator i = m_map.find(name);
00248         if( i == m_map.end() )
00249                 return false;
00250 
00251         m_dnAttr = name;
00252         return true;
00253 }
00254 
00255 bool ContactLdif::SetObjectClass(const LdifAttribute &name,
00256                                  const std::string &objectClass)
00257 {
00258         AccessMapType::iterator i = m_map.find(name);
00259         if( i == m_map.end() )
00260                 return false;
00261 
00262         LdifAttribute key = i->first;
00263         AccessPair pair = i->second;
00264         m_map.erase(key);
00265         key.objectClass = objectClass;
00266         m_map[key] = pair;
00267         return true;
00268 }
00269 
00270 bool ContactLdif::SetObjectOrder(const LdifAttribute &name, int order)
00271 {
00272         AccessMapType::iterator i = m_map.find(name);
00273         if( i == m_map.end() )
00274                 return false;
00275 
00276         LdifAttribute key = i->first;
00277         AccessPair pair = i->second;
00278         m_map.erase(key);
00279         key.order = order;
00280         m_map[key] = pair;
00281         return true;
00282 }
00283 
00284 
00285 std::string ContactLdif::Email(const Barry::Contact &con) const
00286 {
00287         return con.GetEmail(m_emailIndex++);
00288 }
00289 
00290 std::string ContactLdif::Phone(const Barry::Contact &con) const
00291 {
00292         return con.Phone;
00293 }
00294 
00295 std::string ContactLdif::Fax(const Barry::Contact &con) const
00296 {
00297         return con.Fax;
00298 }
00299 
00300 std::string ContactLdif::WorkPhone(const Barry::Contact &con) const
00301 {
00302         return con.WorkPhone;
00303 }
00304 
00305 std::string ContactLdif::HomePhone(const Barry::Contact &con) const
00306 {
00307         return con.HomePhone;
00308 }
00309 
00310 std::string ContactLdif::MobilePhone(const Barry::Contact &con) const
00311 {
00312         return con.MobilePhone;
00313 }
00314 
00315 std::string ContactLdif::Pager(const Barry::Contact &con) const
00316 {
00317         return con.Pager;
00318 }
00319 
00320 std::string ContactLdif::PIN(const Barry::Contact &con) const
00321 {
00322         return con.PIN;
00323 }
00324 
00325 std::string ContactLdif::FirstName(const Barry::Contact &con) const
00326 {
00327         return con.FirstName;
00328 }
00329 
00330 std::string ContactLdif::LastName(const Barry::Contact &con) const
00331 {
00332         return con.LastName;
00333 }
00334 
00335 std::string ContactLdif::Company(const Barry::Contact &con) const
00336 {
00337         return con.Company;
00338 }
00339 
00340 std::string ContactLdif::DefaultCommunicationsMethod(const Barry::Contact &con) const
00341 {
00342         return con.DefaultCommunicationsMethod;
00343 }
00344 
00345 std::string ContactLdif::WorkAddress1(const Barry::Contact &con) const
00346 {
00347         return con.WorkAddress.Address1;
00348 }
00349 
00350 std::string ContactLdif::WorkAddress2(const Barry::Contact &con) const
00351 {
00352         return con.WorkAddress.Address2;
00353 }
00354 
00355 std::string ContactLdif::WorkAddress3(const Barry::Contact &con) const
00356 {
00357         return con.WorkAddress.Address3;
00358 }
00359 
00360 std::string ContactLdif::WorkCity(const Barry::Contact &con) const
00361 {
00362         return con.WorkAddress.City;
00363 }
00364 
00365 std::string ContactLdif::WorkProvince(const Barry::Contact &con) const
00366 {
00367         return con.WorkAddress.Province;
00368 }
00369 
00370 std::string ContactLdif::WorkPostalCode(const Barry::Contact &con) const
00371 {
00372         return con.WorkAddress.PostalCode;
00373 }
00374 
00375 std::string ContactLdif::WorkCountry(const Barry::Contact &con) const
00376 {
00377         return con.WorkAddress.Country;
00378 }
00379 
00380 std::string ContactLdif::JobTitle(const Barry::Contact &con) const
00381 {
00382         return con.JobTitle;
00383 }
00384 
00385 std::string ContactLdif::PublicKey(const Barry::Contact &con) const
00386 {
00387         return con.PublicKey;
00388 }
00389 
00390 std::string ContactLdif::Notes(const Barry::Contact &con) const
00391 {
00392         return con.Notes;
00393 }
00394 
00395 std::string ContactLdif::WorkPostalAddress(const Barry::Contact &con) const
00396 {
00397         return con.WorkAddress.GetLabel();
00398 }
00399 
00400 std::string ContactLdif::HomePostalAddress(const Barry::Contact &con) const
00401 {
00402         return con.HomeAddress.GetLabel();
00403 }
00404 
00405 std::string ContactLdif::FullName(const Barry::Contact &con) const
00406 {
00407         return con.GetFullName();
00408 }
00409 
00410 std::string ContactLdif::FQDN(const Barry::Contact &con) const
00411 {
00412         std::string FQDN = m_dnAttr.name;
00413         FQDN += "=";
00414 
00415         AccessMapType::const_iterator i = m_map.find(m_dnAttr);
00416         if( i != m_map.end() ) {
00417                 FQDN += (this->*(i->second.read))(con);
00418         }
00419         else {
00420                 FQDN += "unknown";
00421         }
00422 
00423         FQDN += ",";
00424         FQDN += m_baseDN;
00425         return FQDN;
00426 }
00427 
00428 bool ContactLdif::IsArrayFunc(GetFunctionType getf) const
00429 {
00430         // Currently, only the Email getter has array data
00431         if( getf == &ContactLdif::Email )
00432                 return true;
00433         return false;
00434 }
00435 
00436 void ContactLdif::ClearArrayState() const
00437 {
00438         m_emailIndex = 0;
00439 }
00440 
00441 void ContactLdif::SetEmail(Barry::Contact &con, const std::string &val) const
00442 {
00443         con.EmailAddresses.push_back(val);
00444 }
00445 
00446 void ContactLdif::SetPhone(Barry::Contact &con, const std::string &val) const
00447 {
00448         con.Phone = val;
00449 }
00450 
00451 void ContactLdif::SetFax(Barry::Contact &con, const std::string &val) const
00452 {
00453         con.Fax = val;
00454 }
00455 
00456 void ContactLdif::SetWorkPhone(Barry::Contact &con, const std::string &val) const
00457 {
00458         con.WorkPhone = val;
00459 }
00460 
00461 void ContactLdif::SetHomePhone(Barry::Contact &con, const std::string &val) const
00462 {
00463         con.HomePhone = val;
00464 }
00465 
00466 void ContactLdif::SetMobilePhone(Barry::Contact &con, const std::string &val) const
00467 {
00468         con.MobilePhone = val;
00469 }
00470 
00471 void ContactLdif::SetPager(Barry::Contact &con, const std::string &val) const
00472 {
00473         con.Pager = val;
00474 }
00475 
00476 void ContactLdif::SetPIN(Barry::Contact &con, const std::string &val) const
00477 {
00478         con.PIN = val;
00479 }
00480 
00481 void ContactLdif::SetFirstName(Barry::Contact &con, const std::string &val) const
00482 {
00483         con.FirstName = val;
00484 }
00485 
00486 void ContactLdif::SetLastName(Barry::Contact &con, const std::string &val) const
00487 {
00488         con.LastName = val;
00489 }
00490 
00491 void ContactLdif::SetCompany(Barry::Contact &con, const std::string &val) const
00492 {
00493         con.Company = val;
00494 }
00495 
00496 void ContactLdif::SetDefaultCommunicationsMethod(Barry::Contact &con, const std::string &val) const
00497 {
00498         con.DefaultCommunicationsMethod = val;
00499 }
00500 
00501 void ContactLdif::SetWorkAddress1(Barry::Contact &con, const std::string &val) const
00502 {
00503         con.WorkAddress.Address1 = val;
00504 }
00505 
00506 void ContactLdif::SetWorkAddress2(Barry::Contact &con, const std::string &val) const
00507 {
00508         con.WorkAddress.Address2 = val;
00509 }
00510 
00511 void ContactLdif::SetWorkAddress3(Barry::Contact &con, const std::string &val) const
00512 {
00513         con.WorkAddress.Address3 = val;
00514 }
00515 
00516 void ContactLdif::SetWorkCity(Barry::Contact &con, const std::string &val) const
00517 {
00518         con.WorkAddress.City = val;
00519 }
00520 
00521 void ContactLdif::SetWorkProvince(Barry::Contact &con, const std::string &val) const
00522 {
00523         con.WorkAddress.Province = val;
00524 }
00525 
00526 void ContactLdif::SetWorkPostalCode(Barry::Contact &con, const std::string &val) const
00527 {
00528         con.WorkAddress.PostalCode = val;
00529 }
00530 
00531 void ContactLdif::SetWorkCountry(Barry::Contact &con, const std::string &val) const
00532 {
00533         con.WorkAddress.Country = val;
00534 }
00535 
00536 void ContactLdif::SetJobTitle(Barry::Contact &con, const std::string &val) const
00537 {
00538         con.JobTitle = val;
00539 }
00540 
00541 void ContactLdif::SetPublicKey(Barry::Contact &con, const std::string &val) const
00542 {
00543         con.PublicKey = val;
00544 }
00545 
00546 void ContactLdif::SetNotes(Barry::Contact &con, const std::string &val) const
00547 {
00548         con.Notes = val;
00549 }
00550 
00551 void ContactLdif::SetWorkPostalAddress(Barry::Contact &con, const std::string &val) const
00552 {
00553         // FIXME;
00554 //      throw std::runtime_error("SetWorkPostalAddress() not implemented");
00555 //      std::cout << "SetWorkPostalAddress() not implemented: " << val << std::endl;
00556 }
00557 
00558 void ContactLdif::SetHomePostalAddress(Barry::Contact &con, const std::string &val) const
00559 {
00560         // FIXME;
00561 //      throw std::runtime_error("SetHomePostalAddress() not implemented");
00562 //      std::cout << "SetHomePostalAddress() not implemented: " << val << std::endl;
00563 }
00564 
00565 void ContactLdif::SetFullName(Barry::Contact &con, const std::string &val) const
00566 {
00567         std::string first, last;
00568         Contact::SplitName(val, first, last);
00569         con.FirstName = first;
00570         con.LastName = last;
00571 }
00572 
00573 void ContactLdif::SetFQDN(Barry::Contact &con, const std::string &val) const
00574 {
00575         throw std::runtime_error("not implemented");
00576 }
00577 
00578 
00579 void ContactLdif::ClearHeuristics()
00580 {
00581         m_cn.clear();
00582         m_displayName.clear();
00583         m_sn.clear();
00584         m_givenName.clear();
00585 }
00586 
00587 bool ContactLdif::RunHeuristics(Barry::Contact &con)
00588 {
00589         // start fresh
00590         con.LastName.clear();
00591         con.FirstName.clear();
00592 
00593         // find the best match for name... prefer sn/givenName if available
00594         if( m_sn.size() ) {
00595                 con.LastName = m_sn;
00596         }
00597         if( m_givenName.size() ) {
00598                 con.FirstName = m_givenName;
00599         }
00600 
00601         if( !con.LastName.size() || !con.FirstName.size() ) {
00602                 std::string first, last;
00603 
00604                 // still don't have a complete name, check cn first
00605                 if( m_cn.size() ) {
00606                         Contact::SplitName(m_cn, first, last);
00607                         if( !con.LastName.size() && last.size() )
00608                                 con.LastName = last;
00609                         if( !con.FirstName.size() && first.size() )
00610                                 con.FirstName = first;
00611                 }
00612 
00613                 // displayName is last chance
00614                 if( m_displayName.size() ) {
00615                         Contact::SplitName(m_displayName, first, last);
00616                         if( !con.LastName.size() && last.size() )
00617                                 con.LastName = last;
00618                         if( !con.FirstName.size() && first.size() )
00619                                 con.FirstName = first;
00620                 }
00621         }
00622 
00623         return con.LastName.size() && con.FirstName.size();
00624 }
00625 
00626 
00627 //
00628 // DumpLdif
00629 //
00630 /// Output contact data to os in LDAP LDIF format.
00631 ///
00632 void ContactLdif::DumpLdif(std::ostream &os,
00633                        const Barry::Contact &con) const
00634 {
00635         // start fresh
00636         ClearArrayState();
00637 
00638         // setup stream state
00639         std::ios::fmtflags oldflags = os.setf(std::ios::left);
00640         char fill = os.fill(' ');
00641 
00642         if( FirstName(con).size() == 0 && LastName(con).size() == 0 )
00643                 return;                 // nothing to do
00644 
00645         os << "# Contact 0x" << std::hex << con.GetID() << ", "
00646                 << FullName(con) << "\n";
00647 
00648         // cycle through the map
00649         for(    AccessMapType::const_iterator b = m_map.begin();
00650                 b != m_map.end();
00651                 ++b )
00652         {
00653                 // print only fields with data
00654                 std::string field;
00655 
00656                 do {
00657                         field = (this->*(b->second.read))(con);
00658                         if( field.size() ) {
00659                                 os << b->first.name << MakeLdifData(field) << "\n";
00660                                 if( b->first.objectClass.size() )
00661                                         os << "objectClass: " << b->first.objectClass << "\n";
00662                         }
00663                 } while( IsArrayFunc(b->second.read) && field.size() );
00664         }
00665 
00666         os << "objectClass: inetOrgPerson\n";
00667 
00668         // last line must be empty
00669         os << "\n";
00670 
00671         // cleanup the stream
00672         os.flags(oldflags);
00673         os.fill(fill);
00674 }
00675 
00676 bool ContactLdif::ReadLdif(std::istream &is, Barry::Contact &con)
00677 {
00678         std::string line;
00679 
00680         // start fresh
00681         con.Clear();
00682         ClearHeuristics();
00683 
00684         // search for beginning dn: line
00685         bool found = false;
00686         while( std::getline(is, line) ) {
00687                 if( strncmp(line.c_str(), "dn: ", 4) == 0 ) {
00688                         found = true;
00689                         break;
00690                 }
00691         }
00692         if( !found )
00693                 return false;
00694 
00695         // storage for various name styles
00696         std::string coded, decode, attr, data;
00697         bool b64field = false;
00698 
00699         // read ldif lines until empty line is found
00700         while( getline(is, line) && line.size() ) {
00701 
00702                 if( b64field ) {
00703                         // processing a base64 encoded field
00704                         if( line[0] == ' ' ) {
00705                                 coded += "\n";
00706                                 coded += line;
00707                                 continue;
00708                         }
00709                         else {
00710                                 // end of base64 block... ignore errors,
00711                                 // and attempt to save everything decodable...
00712                                 // the LDAP server sometimes returns incomplete
00713                                 // base64 encoding, but otherwise the data is fine
00714                                 base64_decode(coded, decode);
00715                                 DoWrite(con, attr, decode);
00716                                 coded.clear();
00717                                 b64field = false;
00718                         }
00719                         // fall through to process new line
00720                 }
00721 
00722 
00723                 // split into attribute / data
00724                 std::string::size_type delim = line.find(':'), dstart;
00725                 if( delim == std::string::npos )
00726                         continue;
00727 
00728                 attr.assign(line, 0, delim);
00729                 dstart = delim + 1;
00730                 while( line[dstart] == ' ' || line[dstart] == ':' )
00731                         dstart++;
00732                 data = line.substr(dstart);
00733 
00734                 // is this data base64 encoded?
00735                 if( line[delim + 1] == ':' ) {
00736                         coded = data;
00737                         b64field = true;
00738                         continue;
00739                 }
00740 
00741                 DoWrite(con, attr, data);
00742         }
00743 
00744         if( b64field ) {
00745                 // clean up base64 decoding... ignore errors, see above comment
00746                 base64_decode(coded, decode);
00747                 DoWrite(con, attr, decode);
00748                 coded.clear();
00749                 b64field = false;
00750         }
00751 
00752         return RunHeuristics(con);
00753 }
00754 
00755 void ContactLdif::DumpMap(std::ostream &os) const
00756 {
00757         std::ios::fmtflags oldflags = os.setf(std::ios::left);
00758         char fill = os.fill(' ');
00759 
00760         os << "ContactLdif Mapping:\n";
00761 
00762         // cycle through the map
00763         for(    AccessMapType::const_iterator b = m_map.begin();
00764                 b != m_map.end();
00765                 ++b )
00766         {
00767                 os << "   " << std::left << std::setw(20) << b->first.name
00768                    << "->  " << GetFieldReadName(b->second.read)
00769                    << " / " << GetFieldWriteName(b->second.write) << "\n";
00770 
00771                 // find read/write names
00772 
00773                 if( b->first.objectClass.size() ) {
00774                         os << "   " << std::setw(20) << " "
00775                            << "objectClass: " << b->first.objectClass << "\n";
00776                 }
00777         }
00778 
00779         os << "   >>> DN attribute: " << m_dnAttr.name << "\n";
00780 
00781         // cleanup the stream
00782         os.flags(oldflags);
00783         os.fill(fill);
00784 }
00785 
00786 std::string ContactLdif::MakeLdifData(const std::string &str)
00787 {
00788         std::string data = ":";
00789 
00790         if( NeedsEncoding(str) ) {
00791                 std::string b64;
00792                 base64_encode(str, b64);
00793 
00794                 data += ": ";
00795                 data += b64;
00796         }
00797         else {
00798                 data += " ";
00799                 data += str;
00800         }
00801 
00802         return data;
00803 }
00804 
00805 //
00806 // RFC 2849
00807 //
00808 // Must not contain:
00809 //      0x00 (NUL), 0x0a (LF), 0x0d (CR), or anything greater than 0x7f
00810 //
00811 // First char must meet above criteria, plus must not be:
00812 //      0x20 (SPACE), 0x3a (colon), 0x3c ('<')
00813 //
00814 bool ContactLdif::NeedsEncoding(const std::string &str)
00815 {
00816         for( std::string::size_type i = 0; i < str.size(); i++ ) {
00817                 unsigned char c = str[i];
00818 
00819                 switch( c )
00820                 {
00821                 case 0x00:
00822                 case 0x0a:
00823                 case 0x0d:
00824                         return true;
00825 
00826                 case 0x20:
00827                 case 0x3a:
00828                 case 0x3c:
00829                         if( i == 0 )
00830                                 return true;
00831                 }
00832 
00833                 if( c > 0x7f )
00834                         return true;
00835         }
00836         return false;
00837 }
00838 
00839 } // namespace Barry
00840 

Generated on 29 Mar 2010 for Barry by  doxygen 1.6.1