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
},
74 &ContactLdif::Title
, &ContactLdif::SetTitle
},
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::Title
, &ContactLdif::SetTitle
);
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
345 std::string
ContactLdif::Address2(const Barry::Contact
&con
) const
350 std::string
ContactLdif::Address3(const Barry::Contact
&con
) const
355 std::string
ContactLdif::City(const Barry::Contact
&con
) const
360 std::string
ContactLdif::Province(const Barry::Contact
&con
) const
365 std::string
ContactLdif::PostalCode(const Barry::Contact
&con
) const
367 return con
.PostalCode
;
370 std::string
ContactLdif::Country(const Barry::Contact
&con
) const
375 std::string
ContactLdif::Title(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
.GetPostalAddress();
395 std::string
ContactLdif::FullName(const Barry::Contact
&con
) const
397 std::string Full
= con
.FirstName
;
400 Full
+= con
.LastName
;
404 std::string
ContactLdif::FQDN(const Barry::Contact
&con
) const
406 std::string FQDN
= m_dnAttr
.name
;
409 AccessMapType::const_iterator i
= m_map
.find(m_dnAttr
);
410 if( i
!= m_map
.end() ) {
411 FQDN
+= (this->*(i
->second
.read
))(con
);
422 void ContactLdif::SetEmail(Barry::Contact
&con
, const std::string
&val
) const
427 void ContactLdif::SetPhone(Barry::Contact
&con
, const std::string
&val
) const
432 void ContactLdif::SetFax(Barry::Contact
&con
, const std::string
&val
) const
437 void ContactLdif::SetWorkPhone(Barry::Contact
&con
, const std::string
&val
) const
442 void ContactLdif::SetHomePhone(Barry::Contact
&con
, const std::string
&val
) const
447 void ContactLdif::SetMobilePhone(Barry::Contact
&con
, const std::string
&val
) const
449 con
.MobilePhone
= val
;
452 void ContactLdif::SetPager(Barry::Contact
&con
, const std::string
&val
) const
457 void ContactLdif::SetPIN(Barry::Contact
&con
, const std::string
&val
) const
462 void ContactLdif::SetFirstName(Barry::Contact
&con
, const std::string
&val
) const
467 void ContactLdif::SetLastName(Barry::Contact
&con
, const std::string
&val
) const
472 void ContactLdif::SetCompany(Barry::Contact
&con
, const std::string
&val
) const
477 void ContactLdif::SetDefaultCommunicationsMethod(Barry::Contact
&con
, const std::string
&val
) const
479 con
.DefaultCommunicationsMethod
= val
;
482 void ContactLdif::SetAddress1(Barry::Contact
&con
, const std::string
&val
) const
487 void ContactLdif::SetAddress2(Barry::Contact
&con
, const std::string
&val
) const
492 void ContactLdif::SetAddress3(Barry::Contact
&con
, const std::string
&val
) const
497 void ContactLdif::SetCity(Barry::Contact
&con
, const std::string
&val
) const
502 void ContactLdif::SetProvince(Barry::Contact
&con
, const std::string
&val
) const
507 void ContactLdif::SetPostalCode(Barry::Contact
&con
, const std::string
&val
) const
509 con
.PostalCode
= val
;
512 void ContactLdif::SetCountry(Barry::Contact
&con
, const std::string
&val
) const
517 void ContactLdif::SetTitle(Barry::Contact
&con
, const std::string
&val
) const
522 void ContactLdif::SetPublicKey(Barry::Contact
&con
, const std::string
&val
) const
527 void ContactLdif::SetNotes(Barry::Contact
&con
, const std::string
&val
) const
532 void ContactLdif::SetPostalAddress(Barry::Contact
&con
, const std::string
&val
) const
535 // throw std::runtime_error("SetPostalAddress() not implemented");
536 // std::cout << "SetPostalAddress() not implemented: " << val << std::endl;
539 void ContactLdif::SetFullName(Barry::Contact
&con
, const std::string
&val
) const
541 std::string first
, last
;
542 Contact::SplitName(val
, first
, last
);
543 con
.FirstName
= first
;
547 void ContactLdif::SetFQDN(Barry::Contact
&con
, const std::string
&val
) const
549 throw std::runtime_error("not implemented");
553 void ContactLdif::ClearHeuristics()
556 m_displayName
.clear();
561 bool ContactLdif::RunHeuristics(Barry::Contact
&con
)
564 con
.LastName
.clear();
565 con
.FirstName
.clear();
567 // find the best match for name... prefer sn/givenName if available
571 if( m_givenName
.size() ) {
572 con
.FirstName
= m_givenName
;
575 if( !con
.LastName
.size() || !con
.FirstName
.size() ) {
576 std::string first
, last
;
578 // still don't have a complete name, check cn first
580 Contact::SplitName(m_cn
, first
, last
);
581 if( !con
.LastName
.size() && last
.size() )
583 if( !con
.FirstName
.size() && first
.size() )
584 con
.FirstName
= first
;
587 // displayName is last chance
588 if( m_displayName
.size() ) {
589 Contact::SplitName(m_displayName
, first
, last
);
590 if( !con
.LastName
.size() && last
.size() )
592 if( !con
.FirstName
.size() && first
.size() )
593 con
.FirstName
= first
;
597 return con
.LastName
.size() && con
.FirstName
.size();
604 /// Output contact data to os in LDAP LDIF format.
606 void ContactLdif::DumpLdif(std::ostream
&os
,
607 const Barry::Contact
&con
) const
609 std::ios::fmtflags oldflags
= os
.setf(std::ios::left
);
610 char fill
= os
.fill(' ');
612 if( FirstName(con
).size() == 0 && LastName(con
).size() == 0 )
613 return; // nothing to do
615 os
<< "# Contact 0x" << std::hex
<< con
.GetID() << ", "
616 << FullName(con
) << "\n";
618 // cycle through the map
619 for( AccessMapType::const_iterator b
= m_map
.begin();
623 // print only fields with data
624 const std::string field
= (this->*(b
->second
.read
))(con
);
626 os
<< b
->first
.name
<< MakeLdifData(field
) << "\n";
627 if( b
->first
.objectClass
.size() )
628 os
<< "objectClass: " << b
->first
.objectClass
<< "\n";
632 os
<< "objectClass: inetOrgPerson\n";
634 // last line must be empty
637 // cleanup the stream
642 bool ContactLdif::ReadLdif(std::istream
&is
, Barry::Contact
&con
)
650 // search for beginning dn: line
652 while( std::getline(is
, line
) ) {
653 if( strncmp(line
.c_str(), "dn: ", 4) == 0 ) {
661 // storage for various name styles
662 std::string coded
, decode
, attr
, data
;
663 bool b64field
= false;
665 // read ldif lines until empty line is found
666 while( getline(is
, line
) && line
.size() ) {
669 // processing a base64 encoded field
670 if( line
[0] == ' ' ) {
676 // end of base64 block... ignore errors,
677 // and attempt to save everything decodable...
678 // the LDAP server sometimes returns incomplete
679 // base64 encoding, but otherwise the data is fine
680 base64_decode(coded
, decode
);
681 DoWrite(con
, attr
, decode
);
685 // fall through to process new line
689 // split into attribute / data
690 std::string::size_type delim
= line
.find(':'), dstart
;
691 if( delim
== std::string::npos
)
694 attr
.assign(line
, 0, delim
);
696 while( line
[dstart
] == ' ' || line
[dstart
] == ':' )
698 data
= line
.substr(dstart
);
700 // is this data base64 encoded?
701 if( line
[delim
+ 1] == ':' ) {
707 DoWrite(con
, attr
, data
);
711 // clean up base64 decoding... ignore errors, see above comment
712 base64_decode(coded
, decode
);
713 DoWrite(con
, attr
, decode
);
718 return RunHeuristics(con
);
721 void ContactLdif::DumpMap(std::ostream
&os
) const
723 std::ios::fmtflags oldflags
= os
.setf(std::ios::left
);
724 char fill
= os
.fill(' ');
726 os
<< "ContactLdif Mapping:\n";
728 // cycle through the map
729 for( AccessMapType::const_iterator b
= m_map
.begin();
733 os
<< " " << std::left
<< std::setw(20) << b
->first
.name
734 << "-> " << GetFieldReadName(b
->second
.read
)
735 << " / " << GetFieldWriteName(b
->second
.write
) << "\n";
737 // find read/write names
739 if( b
->first
.objectClass
.size() ) {
740 os
<< " " << std::setw(20) << " "
741 << "objectClass: " << b
->first
.objectClass
<< "\n";
745 os
<< " >>> DN attribute: " << m_dnAttr
.name
<< "\n";
747 // cleanup the stream
752 std::string
ContactLdif::MakeLdifData(const std::string
&str
)
754 std::string data
= ":";
756 if( NeedsEncoding(str
) ) {
758 base64_encode(str
, b64
);
775 // 0x00 (NUL), 0x0a (LF), 0x0d (CR), or anything greater than 0x7f
777 // First char must meet above criteria, plus must not be:
778 // 0x20 (SPACE), 0x3a (colon), 0x3c ('<')
780 bool ContactLdif::NeedsEncoding(const std::string
&str
)
782 for( std::string::size_type i
= 0; i
< str
.size(); i
++ ) {
783 unsigned char c
= str
[i
];