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

Generated on Wed Sep 24 21:27:31 2008 for Barry by  doxygen 1.5.1