3 /// Routines for reading and writing LDAP LDIF data.
7 Copyright (C) 2005-2007, 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.
29 #define __DEBUG_MODE__
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
},
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
},
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
},
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
},
89 bool ContactLdif::LdifAttribute::operator<(const LdifAttribute
&other
) const
91 // the dn attribute always comes first in LDIF output
93 if( other
.name
== "dn" )
94 return false; // both dn, so equal
97 else if( other
.name
== "dn" )
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 ///////////////////////////////////////////////////////////////////////////////
113 ContactLdif::ContactLdif(const std::string
&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
142 Hook("displayName", &m_displayName
);
144 Hook("givenName", &m_givenName
);
146 // set default DN attribute
150 ContactLdif::~ContactLdif()
154 void ContactLdif::DoWrite(Barry::Contact
&con
,
155 const std::string
&attr
,
156 const std::string
&data
)
159 if( attr
.size() == 0 || data
.size() == 0 )
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
)
190 std::string
ContactLdif::GetFieldReadName(GetFunctionType read
) const
192 for( const NameToFunc
*n
= FieldMap
; n
->name
; n
++ ) {
193 if( read
== n
->read
)
199 std::string
ContactLdif::GetFieldWriteName(SetFunctionType write
) const
201 for( const NameToFunc
*n
= FieldMap
; n
->name
; n
++ ) {
202 if( write
== n
->write
)
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
)
216 Map(ldifname
, read
->read
, write
->write
);
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
);
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() )
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() )
257 LdifAttribute key
= i
->first
;
258 AccessPair pair
= i
->second
;
260 key
.objectClass
= objectClass
;
265 bool ContactLdif::SetObjectOrder(const LdifAttribute
&name
, int order
)
267 AccessMapType::iterator i
= m_map
.find(name
);
268 if( i
== m_map
.end() )
271 LdifAttribute key
= i
->first
;
272 AccessPair pair
= i
->second
;
280 std::string
ContactLdif::Email(const Barry::Contact
&con
) const
285 std::string
ContactLdif::Phone(const Barry::Contact
&con
) const
290 std::string
ContactLdif::Fax(const Barry::Contact
&con
) const
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
315 std::string
ContactLdif::PIN(const Barry::Contact
&con
) const
320 std::string
ContactLdif::FirstName(const Barry::Contact
&con
) const
322 return con
.FirstName
;
325 std::string
ContactLdif::LastName(const Barry::Contact
&con
) const
330 std::string
ContactLdif::Company(const Barry::Contact
&con
) const
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
380 std::string
ContactLdif::PublicKey(const Barry::Contact
&con
) const
382 return con
.PublicKey
;
385 std::string
ContactLdif::Notes(const Barry::Contact
&con
) const
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
;
405 AccessMapType::const_iterator i
= m_map
.find(m_dnAttr
);
406 if( i
!= m_map
.end() ) {
407 FQDN
+= (this->*(i
->second
.read
))(con
);
418 void ContactLdif::SetEmail(Barry::Contact
&con
, const std::string
&val
) const
423 void ContactLdif::SetPhone(Barry::Contact
&con
, const std::string
&val
) const
428 void ContactLdif::SetFax(Barry::Contact
&con
, const std::string
&val
) const
433 void ContactLdif::SetWorkPhone(Barry::Contact
&con
, const std::string
&val
) const
438 void ContactLdif::SetHomePhone(Barry::Contact
&con
, const std::string
&val
) const
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
453 void ContactLdif::SetPIN(Barry::Contact
&con
, const std::string
&val
) const
458 void ContactLdif::SetFirstName(Barry::Contact
&con
, const std::string
&val
) const
463 void ContactLdif::SetLastName(Barry::Contact
&con
, const std::string
&val
) const
468 void ContactLdif::SetCompany(Barry::Contact
&con
, const std::string
&val
) const
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
518 void ContactLdif::SetPublicKey(Barry::Contact
&con
, const std::string
&val
) const
523 void ContactLdif::SetNotes(Barry::Contact
&con
, const std::string
&val
) const
528 void ContactLdif::SetPostalAddress(Barry::Contact
&con
, const std::string
&val
) const
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
;
543 void ContactLdif::SetFQDN(Barry::Contact
&con
, const std::string
&val
) const
545 throw std::runtime_error("not implemented");
549 void ContactLdif::ClearHeuristics()
552 m_displayName
.clear();
557 bool ContactLdif::RunHeuristics(Barry::Contact
&con
)
560 con
.LastName
.clear();
561 con
.FirstName
.clear();
563 // find the best match for name... prefer sn/givenName if available
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
576 Contact::SplitName(m_cn
, first
, last
);
577 if( !con
.LastName
.size() && last
.size() )
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() )
588 if( !con
.FirstName
.size() && first
.size() )
589 con
.FirstName
= first
;
593 return con
.LastName
.size() && con
.FirstName
.size();
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();
619 // print only fields with data
620 const std::string field
= (this->*(b
->second
.read
))(con
);
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
633 // cleanup the stream
638 bool ContactLdif::ReadLdif(std::istream
&is
, Barry::Contact
&con
)
646 // search for beginning dn: line
648 while( std::getline(is
, line
) ) {
649 if( strncmp(line
.c_str(), "dn: ", 4) == 0 ) {
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() ) {
665 // processing a base64 encoded field
666 if( line
[0] == ' ' ) {
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
);
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
)
690 attr
.assign(line
, 0, delim
);
692 while( line
[dstart
] == ' ' || line
[dstart
] == ':' )
694 data
= line
.substr(dstart
);
696 // is this data base64 encoded?
697 if( line
[delim
+ 1] == ':' ) {
703 DoWrite(con
, attr
, data
);
707 // clean up base64 decoding... ignore errors, see above comment
708 base64_decode(coded
, decode
);
709 DoWrite(con
, attr
, decode
);
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();
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
748 std::string
ContactLdif::MakeLdifData(const std::string
&str
)
750 std::string data
= ":";
752 if( NeedsEncoding(str
) ) {
754 base64_encode(str
, b64
);
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
];