i18n work in progress... desperately needs some housecleaning.
[barry.git] / src / ldif.cc
blob845114deb2b95c0e68f8d093c0bf39a7c5664f91
1 ///
2 /// \file ldif.cc
3 /// Routines for reading and writing LDAP LDIF data.
4 ///
6 /*
7 Copyright (C) 2005-2008, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "ldif.h"
23 #include "record.h"
24 #include "base64.h"
25 #include <stdexcept>
26 #include <iostream>
27 #include <iomanip>
29 #define __DEBUG_MODE__
30 #include "debug.h"
32 namespace Barry {
34 const ContactLdif::NameToFunc ContactLdif::FieldMap[] = {
35 { "Email", "Email address",
36 &ContactLdif::Email, &ContactLdif::SetEmail },
37 { "Phone", "Phone number",
38 &ContactLdif::Phone, &ContactLdif::SetPhone },
39 { "Fax", "Fax number",
40 &ContactLdif::Fax, &ContactLdif::SetFax },
41 { "WorkPhone", "Work phone number",
42 &ContactLdif::WorkPhone, &ContactLdif::SetWorkPhone },
43 { "HomePhone", "Home phone number",
44 &ContactLdif::HomePhone, &ContactLdif::SetHomePhone },
45 { "MobilePhone", "Mobile phone number",
46 &ContactLdif::MobilePhone, &ContactLdif::SetMobilePhone },
47 { "Pager", "Pager number",
48 &ContactLdif::Pager, &ContactLdif::SetPager },
49 { "PIN", "PIN",
50 &ContactLdif::PIN, &ContactLdif::SetPIN },
51 { "FirstName", "First name",
52 &ContactLdif::FirstName, &ContactLdif::SetFirstName },
53 { "LastName", "Last name",
54 &ContactLdif::LastName, &ContactLdif::SetLastName },
55 { "Company", "Company name",
56 &ContactLdif::Company, &ContactLdif::SetCompany },
57 { "DefaultCommunicationsMethod", "Default communications method",
58 &ContactLdif::DefaultCommunicationsMethod, &ContactLdif::SetDefaultCommunicationsMethod },
59 { "Address1", "Address, line 1",
60 &ContactLdif::Address1, &ContactLdif::SetAddress1 },
61 { "Address2", "Address, line 2",
62 &ContactLdif::Address2, &ContactLdif::SetAddress2 },
63 { "Address3", "Address, line 3",
64 &ContactLdif::Address3, &ContactLdif::SetAddress3 },
65 { "City", "City",
66 &ContactLdif::City, &ContactLdif::SetCity },
67 { "Province", "Province / State",
68 &ContactLdif::Province, &ContactLdif::SetProvince },
69 { "PostalCode", "Postal / ZIP code",
70 &ContactLdif::PostalCode, &ContactLdif::SetPostalCode },
71 { "Country", "Country",
72 &ContactLdif::Country, &ContactLdif::SetCountry },
73 { "JobTitle", "Job Title",
74 &ContactLdif::JobTitle, &ContactLdif::SetJobTitle },
75 { "PublicKey", "Public key",
76 &ContactLdif::PublicKey, &ContactLdif::SetPublicKey },
77 { "Notes", "Notes",
78 &ContactLdif::Notes, &ContactLdif::SetNotes },
79 { "PostalAddress", "Mailing address (includes address lines, city, province, country, and postal code)",
80 &ContactLdif::PostalAddress, &ContactLdif::SetPostalAddress },
81 { "FullName", "First + Last names",
82 &ContactLdif::FullName, &ContactLdif::SetFullName },
83 { "FQDN", "Fully qualified domain name",
84 &ContactLdif::FQDN, &ContactLdif::SetFQDN },
85 { 0, 0, 0 }
89 bool ContactLdif::LdifAttribute::operator<(const LdifAttribute &other) const
91 // the dn attribute always comes first in LDIF output
92 if( name == "dn" ) {
93 if( other.name == "dn" )
94 return false; // both dn, so equal
95 return true;
97 else if( other.name == "dn" )
98 return false;
100 return (order < other.order && name != other.name) ||
101 (order == other.order && name < other.name);
104 bool ContactLdif::LdifAttribute::operator==(const LdifAttribute &other) const
106 return name == other.name;
110 ///////////////////////////////////////////////////////////////////////////////
111 // ContactLdif class
113 ContactLdif::ContactLdif(const std::string &baseDN)
114 : m_baseDN(baseDN)
116 // setup some sane defaults
117 Map("mail", &ContactLdif::Email, &ContactLdif::SetEmail);
118 Map("facsimileTelephoneNumber", &ContactLdif::Fax, &ContactLdif::SetFax);
119 Map("telephoneNumber", &ContactLdif::WorkPhone, &ContactLdif::SetWorkPhone);
120 Map("homePhone", &ContactLdif::HomePhone, &ContactLdif::SetHomePhone);
121 Map("mobile", &ContactLdif::MobilePhone, &ContactLdif::SetMobilePhone);
122 Map("pager", &ContactLdif::Pager, &ContactLdif::SetPager);
123 Map("l", &ContactLdif::City, &ContactLdif::SetCity);
124 Map("st", &ContactLdif::Province, &ContactLdif::SetProvince);
125 Map("postalCode", &ContactLdif::PostalCode, &ContactLdif::SetPostalCode);
126 Map("o", &ContactLdif::Company, &ContactLdif::SetCompany);
127 Map("c", &ContactLdif::Country, &ContactLdif::SetCountry);
128 SetObjectClass("c", "country");
130 Map("title", &ContactLdif::JobTitle, &ContactLdif::SetJobTitle);
131 Map("dn", &ContactLdif::FQDN, &ContactLdif::SetFQDN);
132 Map("displayName", &ContactLdif::FullName, &ContactLdif::SetFullName);
133 Map("cn", &ContactLdif::FullName, &ContactLdif::SetFullName);
134 Map("sn", &ContactLdif::LastName, &ContactLdif::SetLastName);
135 Map("givenName", &ContactLdif::FirstName, &ContactLdif::SetFirstName);
136 Map("street", &ContactLdif::Address1, &ContactLdif::SetAddress1);
137 Map("postalAddress", &ContactLdif::PostalAddress, &ContactLdif::SetPostalAddress);
138 Map("note", &ContactLdif::Notes, &ContactLdif::SetNotes);
140 // add heuristics hooks
141 Hook("cn", &m_cn);
142 Hook("displayName", &m_displayName);
143 Hook("sn", &m_sn);
144 Hook("givenName", &m_givenName);
146 // set default DN attribute
147 SetDNAttr("cn");
150 ContactLdif::~ContactLdif()
154 void ContactLdif::DoWrite(Barry::Contact &con,
155 const std::string &attr,
156 const std::string &data)
158 // valid?
159 if( attr.size() == 0 || data.size() == 0 )
160 return;
162 // now have attr/data pair, check hooks:
163 HookMapType::iterator hook = m_hookMap.find(attr);
164 if( hook != m_hookMap.end() ) {
165 *(hook->second) = data;
168 // run according to map
169 AccessMapType::iterator acc = m_map.find(attr);
170 if( acc != m_map.end() ) {
171 (this->*(acc->second.write))(con, data);
175 void ContactLdif::Hook(const std::string &ldifname, std::string *var)
177 m_hookMap[ldifname] = var;
180 const ContactLdif::NameToFunc*
181 ContactLdif::GetField(const std::string &fieldname) const
183 for( const NameToFunc *n = FieldMap; n->name; n++ ) {
184 if( fieldname == n->name )
185 return n;
187 return 0;
190 std::string ContactLdif::GetFieldReadName(GetFunctionType read) const
192 for( const NameToFunc *n = FieldMap; n->name; n++ ) {
193 if( read == n->read )
194 return n->name;
196 return "<unknown>";
199 std::string ContactLdif::GetFieldWriteName(SetFunctionType write) const
201 for( const NameToFunc *n = FieldMap; n->name; n++ ) {
202 if( write == n->write )
203 return n->name;
205 return "<unknown>";
208 bool ContactLdif::Map(const LdifAttribute &ldifname,
209 const std::string &readField,
210 const std::string &writeField)
212 const NameToFunc *read = GetField(readField);
213 const NameToFunc *write = GetField(writeField);
214 if( !read || !write )
215 return false;
216 Map(ldifname, read->read, write->write);
217 return true;
220 void ContactLdif::Map(const LdifAttribute &ldifname,
221 GetFunctionType read,
222 SetFunctionType write)
224 m_map[ldifname] = AccessPair(read, write);
227 void ContactLdif::Unmap(const LdifAttribute &ldifname)
229 m_map.erase(ldifname);
233 // SetDNAttr
235 /// Sets the LDIF attribute name to use when constructing the FQDN.
236 /// The FQDN field will take this name, and combine it with the
237 /// baseDN from the constructor to produce a FQDN for the record.
239 bool ContactLdif::SetDNAttr(const LdifAttribute &name)
241 // try to find the attribute in the map
242 AccessMapType::iterator i = m_map.find(name);
243 if( i == m_map.end() )
244 return false;
246 m_dnAttr = name;
247 return true;
250 bool ContactLdif::SetObjectClass(const LdifAttribute &name,
251 const std::string &objectClass)
253 AccessMapType::iterator i = m_map.find(name);
254 if( i == m_map.end() )
255 return false;
257 LdifAttribute key = i->first;
258 AccessPair pair = i->second;
259 m_map.erase(key);
260 key.objectClass = objectClass;
261 m_map[key] = pair;
262 return true;
265 bool ContactLdif::SetObjectOrder(const LdifAttribute &name, int order)
267 AccessMapType::iterator i = m_map.find(name);
268 if( i == m_map.end() )
269 return false;
271 LdifAttribute key = i->first;
272 AccessPair pair = i->second;
273 m_map.erase(key);
274 key.order = order;
275 m_map[key] = pair;
276 return true;
280 std::string ContactLdif::Email(const Barry::Contact &con) const
282 return con.Email;
285 std::string ContactLdif::Phone(const Barry::Contact &con) const
287 return con.Phone;
290 std::string ContactLdif::Fax(const Barry::Contact &con) const
292 return con.Fax;
295 std::string ContactLdif::WorkPhone(const Barry::Contact &con) const
297 return con.WorkPhone;
300 std::string ContactLdif::HomePhone(const Barry::Contact &con) const
302 return con.HomePhone;
305 std::string ContactLdif::MobilePhone(const Barry::Contact &con) const
307 return con.MobilePhone;
310 std::string ContactLdif::Pager(const Barry::Contact &con) const
312 return con.Pager;
315 std::string ContactLdif::PIN(const Barry::Contact &con) const
317 return con.PIN;
320 std::string ContactLdif::FirstName(const Barry::Contact &con) const
322 return con.FirstName;
325 std::string ContactLdif::LastName(const Barry::Contact &con) const
327 return con.LastName;
330 std::string ContactLdif::Company(const Barry::Contact &con) const
332 return con.Company;
335 std::string ContactLdif::DefaultCommunicationsMethod(const Barry::Contact &con) const
337 return con.DefaultCommunicationsMethod;
340 std::string ContactLdif::Address1(const Barry::Contact &con) const
342 return con.WorkAddress.Address1;
345 std::string ContactLdif::Address2(const Barry::Contact &con) const
347 return con.WorkAddress.Address2;
350 std::string ContactLdif::Address3(const Barry::Contact &con) const
352 return con.WorkAddress.Address3;
355 std::string ContactLdif::City(const Barry::Contact &con) const
357 return con.WorkAddress.City;
360 std::string ContactLdif::Province(const Barry::Contact &con) const
362 return con.WorkAddress.Province;
365 std::string ContactLdif::PostalCode(const Barry::Contact &con) const
367 return con.WorkAddress.PostalCode;
370 std::string ContactLdif::Country(const Barry::Contact &con) const
372 return con.WorkAddress.Country;
375 std::string ContactLdif::JobTitle(const Barry::Contact &con) const
377 return con.JobTitle;
380 std::string ContactLdif::PublicKey(const Barry::Contact &con) const
382 return con.PublicKey;
385 std::string ContactLdif::Notes(const Barry::Contact &con) const
387 return con.Notes;
390 std::string ContactLdif::PostalAddress(const Barry::Contact &con) const
392 return con.WorkAddress.GetLabel();
395 std::string ContactLdif::FullName(const Barry::Contact &con) const
397 return con.GetFullName();
400 std::string ContactLdif::FQDN(const Barry::Contact &con) const
402 std::string FQDN = m_dnAttr.name;
403 FQDN += "=";
405 AccessMapType::const_iterator i = m_map.find(m_dnAttr);
406 if( i != m_map.end() ) {
407 FQDN += (this->*(i->second.read))(con);
409 else {
410 FQDN += "unknown";
413 FQDN += ",";
414 FQDN += m_baseDN;
415 return FQDN;
418 void ContactLdif::SetEmail(Barry::Contact &con, const std::string &val) const
420 con.Email = val;
423 void ContactLdif::SetPhone(Barry::Contact &con, const std::string &val) const
425 con.Phone = val;
428 void ContactLdif::SetFax(Barry::Contact &con, const std::string &val) const
430 con.Fax = val;
433 void ContactLdif::SetWorkPhone(Barry::Contact &con, const std::string &val) const
435 con.WorkPhone = val;
438 void ContactLdif::SetHomePhone(Barry::Contact &con, const std::string &val) const
440 con.HomePhone = val;
443 void ContactLdif::SetMobilePhone(Barry::Contact &con, const std::string &val) const
445 con.MobilePhone = val;
448 void ContactLdif::SetPager(Barry::Contact &con, const std::string &val) const
450 con.Pager = val;
453 void ContactLdif::SetPIN(Barry::Contact &con, const std::string &val) const
455 con.PIN = val;
458 void ContactLdif::SetFirstName(Barry::Contact &con, const std::string &val) const
460 con.FirstName = val;
463 void ContactLdif::SetLastName(Barry::Contact &con, const std::string &val) const
465 con.LastName = val;
468 void ContactLdif::SetCompany(Barry::Contact &con, const std::string &val) const
470 con.Company = val;
473 void ContactLdif::SetDefaultCommunicationsMethod(Barry::Contact &con, const std::string &val) const
475 con.DefaultCommunicationsMethod = val;
478 void ContactLdif::SetAddress1(Barry::Contact &con, const std::string &val) const
480 con.WorkAddress.Address1 = val;
483 void ContactLdif::SetAddress2(Barry::Contact &con, const std::string &val) const
485 con.WorkAddress.Address2 = val;
488 void ContactLdif::SetAddress3(Barry::Contact &con, const std::string &val) const
490 con.WorkAddress.Address3 = val;
493 void ContactLdif::SetCity(Barry::Contact &con, const std::string &val) const
495 con.WorkAddress.City = val;
498 void ContactLdif::SetProvince(Barry::Contact &con, const std::string &val) const
500 con.WorkAddress.Province = val;
503 void ContactLdif::SetPostalCode(Barry::Contact &con, const std::string &val) const
505 con.WorkAddress.PostalCode = val;
508 void ContactLdif::SetCountry(Barry::Contact &con, const std::string &val) const
510 con.WorkAddress.Country = val;
513 void ContactLdif::SetJobTitle(Barry::Contact &con, const std::string &val) const
515 con.JobTitle = val;
518 void ContactLdif::SetPublicKey(Barry::Contact &con, const std::string &val) const
520 con.PublicKey = val;
523 void ContactLdif::SetNotes(Barry::Contact &con, const std::string &val) const
525 con.Notes = val;
528 void ContactLdif::SetPostalAddress(Barry::Contact &con, const std::string &val) const
530 // fixme;
531 // throw std::runtime_error("SetPostalAddress() not implemented");
532 // std::cout << "SetPostalAddress() not implemented: " << val << std::endl;
535 void ContactLdif::SetFullName(Barry::Contact &con, const std::string &val) const
537 std::string first, last;
538 Contact::SplitName(val, first, last);
539 con.FirstName = first;
540 con.LastName = last;
543 void ContactLdif::SetFQDN(Barry::Contact &con, const std::string &val) const
545 throw std::runtime_error("not implemented");
549 void ContactLdif::ClearHeuristics()
551 m_cn.clear();
552 m_displayName.clear();
553 m_sn.clear();
554 m_givenName.clear();
557 bool ContactLdif::RunHeuristics(Barry::Contact &con)
559 // start fresh
560 con.LastName.clear();
561 con.FirstName.clear();
563 // find the best match for name... prefer sn/givenName if available
564 if( m_sn.size() ) {
565 con.LastName = m_sn;
567 if( m_givenName.size() ) {
568 con.FirstName = m_givenName;
571 if( !con.LastName.size() || !con.FirstName.size() ) {
572 std::string first, last;
574 // still don't have a complete name, check cn first
575 if( m_cn.size() ) {
576 Contact::SplitName(m_cn, first, last);
577 if( !con.LastName.size() && last.size() )
578 con.LastName = last;
579 if( !con.FirstName.size() && first.size() )
580 con.FirstName = first;
583 // displayName is last chance
584 if( m_displayName.size() ) {
585 Contact::SplitName(m_displayName, first, last);
586 if( !con.LastName.size() && last.size() )
587 con.LastName = last;
588 if( !con.FirstName.size() && first.size() )
589 con.FirstName = first;
593 return con.LastName.size() && con.FirstName.size();
598 // DumpLdif
600 /// Output contact data to os in LDAP LDIF format.
602 void ContactLdif::DumpLdif(std::ostream &os,
603 const Barry::Contact &con) const
605 std::ios::fmtflags oldflags = os.setf(std::ios::left);
606 char fill = os.fill(' ');
608 if( FirstName(con).size() == 0 && LastName(con).size() == 0 )
609 return; // nothing to do
611 os << "# Contact 0x" << std::hex << con.GetID() << ", "
612 << FullName(con) << "\n";
614 // cycle through the map
615 for( AccessMapType::const_iterator b = m_map.begin();
616 b != m_map.end();
617 ++b )
619 // print only fields with data
620 const std::string field = (this->*(b->second.read))(con);
621 if( field.size() ) {
622 os << b->first.name << MakeLdifData(field) << "\n";
623 if( b->first.objectClass.size() )
624 os << "objectClass: " << b->first.objectClass << "\n";
628 os << "objectClass: inetOrgPerson\n";
630 // last line must be empty
631 os << "\n";
633 // cleanup the stream
634 os.flags(oldflags);
635 os.fill(fill);
638 bool ContactLdif::ReadLdif(std::istream &is, Barry::Contact &con)
640 std::string line;
642 // start fresh
643 con.Clear();
644 ClearHeuristics();
646 // search for beginning dn: line
647 bool found = false;
648 while( std::getline(is, line) ) {
649 if( strncmp(line.c_str(), "dn: ", 4) == 0 ) {
650 found = true;
651 break;
654 if( !found )
655 return false;
657 // storage for various name styles
658 std::string coded, decode, attr, data;
659 bool b64field = false;
661 // read ldif lines until empty line is found
662 while( getline(is, line) && line.size() ) {
664 if( b64field ) {
665 // processing a base64 encoded field
666 if( line[0] == ' ' ) {
667 coded += "\n";
668 coded += line;
669 continue;
671 else {
672 // end of base64 block... ignore errors,
673 // and attempt to save everything decodable...
674 // the LDAP server sometimes returns incomplete
675 // base64 encoding, but otherwise the data is fine
676 base64_decode(coded, decode);
677 DoWrite(con, attr, decode);
678 coded.clear();
679 b64field = false;
681 // fall through to process new line
685 // split into attribute / data
686 std::string::size_type delim = line.find(':'), dstart;
687 if( delim == std::string::npos )
688 continue;
690 attr.assign(line, 0, delim);
691 dstart = delim + 1;
692 while( line[dstart] == ' ' || line[dstart] == ':' )
693 dstart++;
694 data = line.substr(dstart);
696 // is this data base64 encoded?
697 if( line[delim + 1] == ':' ) {
698 coded = data;
699 b64field = true;
700 continue;
703 DoWrite(con, attr, data);
706 if( b64field ) {
707 // clean up base64 decoding... ignore errors, see above comment
708 base64_decode(coded, decode);
709 DoWrite(con, attr, decode);
710 coded.clear();
711 b64field = false;
714 return RunHeuristics(con);
717 void ContactLdif::DumpMap(std::ostream &os) const
719 std::ios::fmtflags oldflags = os.setf(std::ios::left);
720 char fill = os.fill(' ');
722 os << "ContactLdif Mapping:\n";
724 // cycle through the map
725 for( AccessMapType::const_iterator b = m_map.begin();
726 b != m_map.end();
727 ++b )
729 os << " " << std::left << std::setw(20) << b->first.name
730 << "-> " << GetFieldReadName(b->second.read)
731 << " / " << GetFieldWriteName(b->second.write) << "\n";
733 // find read/write names
735 if( b->first.objectClass.size() ) {
736 os << " " << std::setw(20) << " "
737 << "objectClass: " << b->first.objectClass << "\n";
741 os << " >>> DN attribute: " << m_dnAttr.name << "\n";
743 // cleanup the stream
744 os.flags(oldflags);
745 os.fill(fill);
748 std::string ContactLdif::MakeLdifData(const std::string &str)
750 std::string data = ":";
752 if( NeedsEncoding(str) ) {
753 std::string b64;
754 base64_encode(str, b64);
756 data += ": ";
757 data += b64;
759 else {
760 data += " ";
761 data += str;
764 return data;
768 // RFC 2849
770 // Must not contain:
771 // 0x00 (NUL), 0x0a (LF), 0x0d (CR), or anything greater than 0x7f
773 // First char must meet above criteria, plus must not be:
774 // 0x20 (SPACE), 0x3a (colon), 0x3c ('<')
776 bool ContactLdif::NeedsEncoding(const std::string &str)
778 for( std::string::size_type i = 0; i < str.size(); i++ ) {
779 unsigned char c = str[i];
781 switch( c )
783 case 0x00:
784 case 0x0a:
785 case 0x0d:
786 return true;
788 case 0x20:
789 case 0x3a:
790 case 0x3c:
791 if( i == 0 )
792 return true;
795 if( c > 0x7f )
796 return true;
798 return false;
801 } // namespace Barry