1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// $OpenLDAP$
/*
* Copyright 2008-2018 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "LdifReader.h"
#include "LDAPMessage.h"
#include "LDAPEntry.h"
#include "LDAPAttributeList.h"
#include "LDAPAttribute.h"
#include "LDAPUrl.h"
#include "debug.h"
#include <string>
#include <sstream>
#include <stdexcept>
#include <sasl/saslutil.h> // For base64 routines
typedef std::pair<std::string, std::string> stringpair;
LdifReader::LdifReader( std::istream &input )
: m_ldifstream(input), m_lineNumber(0)
{
DEBUG(LDAP_DEBUG_TRACE, "<> LdifReader::LdifReader()" << std::endl);
this->m_version = 0;
// read the first record to find out version and type of the LDIF
this->readNextRecord(true);
this->m_currentIsFirst = true;
}
int LdifReader::readNextRecord( bool first )
{
DEBUG(LDAP_DEBUG_TRACE, "-> LdifReader::readRecord()" << std::endl);
std::string line;
std::string type;
std::string value;
int numLine = 0;
int recordType = 0;
if ( (! first) && this->m_currentIsFirst == true )
{
this->m_currentIsFirst = false;
return m_curRecType;
}
m_currentRecord.clear();
while ( !this->getLdifLine(line) )
{
DEBUG(LDAP_DEBUG_TRACE, " Line: " << line << std::endl );
// skip comments and empty lines between entries
if ( line[0] == '#' || ( numLine == 0 && line.size() == 0 ) )
{
DEBUG(LDAP_DEBUG_TRACE, "skipping empty line or comment" << std::endl );
continue;
}
if ( line.size() == 0 )
{
// End of Entry
break;
}
this->splitLine(line, type, value);
if ( numLine == 0 )
{
if ( type == "version" )
{
std::istringstream valuestream(value);
valuestream >> this->m_version;
if ( this->m_version != 1 ) // there is no other Version than LDIFv1
{
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": Unsuported LDIF Version";
throw( std::runtime_error(err.str()) );
}
continue;
}
if ( type == "dn" ) // Record should start with the DN ...
{
DEBUG(LDAP_DEBUG_TRACE, " Record DN:" << value << std::endl);
}
else if ( type == "include" ) // ... or it might be an "include" line
{
DEBUG(LDAP_DEBUG_TRACE, " Include directive: " << value << std::endl);
if ( this->m_version == 1 )
{
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": \"include\" not allowed in LDIF version 1.";
throw( std::runtime_error(err.str()) );
}
else
{
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": \"include\" not yet suppported.";
throw( std::runtime_error(err.str()) );
}
}
else
{
DEBUG(LDAP_DEBUG_TRACE, " Record doesn't start with a DN"
<< std::endl);
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": LDIF record does not start with a DN.";
throw( std::runtime_error(err.str()) );
}
}
if ( numLine == 1 ) // might contain "changtype" to indicate a change request
{
if ( type == "changetype" )
{
if ( first )
{
this->m_ldifTypeRequest = true;
}
else if (! this->m_ldifTypeRequest )
{
// Change Request in Entry record LDIF, should we accept it?
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": Change Request in an entry-only LDIF.";
throw( std::runtime_error(err.str()) );
}
if ( value == "modify" )
{
recordType = LDAPMsg::MODIFY_REQUEST;
}
else if ( value == "add" )
{
recordType = LDAPMsg::ADD_REQUEST;
}
else if ( value == "delete" )
{
recordType = LDAPMsg::DELETE_REQUEST;
}
else if ( value == "modrdn" )
{
recordType = LDAPMsg::MODRDN_REQUEST;
}
else
{
DEBUG(LDAP_DEBUG_TRACE, " Unknown change request <"
<< value << ">" << std::endl);
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": Unknown changetype: \"" << value << "\".";
throw( std::runtime_error(err.str()) );
}
}
else
{
if ( first )
{
this->m_ldifTypeRequest = false;
}
else if (this->m_ldifTypeRequest )
{
// Entry record in Change record LDIF, should we accept
// it (e.g. as AddRequest)?
}
recordType = LDAPMsg::SEARCH_ENTRY;
}
}
m_currentRecord.push_back( stringpair(type, value) );
numLine++;
}
DEBUG(LDAP_DEBUG_TRACE, "<- LdifReader::readRecord() return: "
<< recordType << std::endl);
m_curRecType = recordType;
return recordType;
}
LDAPEntry LdifReader::getEntryRecord()
{
std::list<stringpair>::const_iterator i = m_currentRecord.begin();
if ( m_curRecType != LDAPMsg::SEARCH_ENTRY )
{
throw( std::runtime_error( "The LDIF record: '" + i->second +
"' is not a valid LDAP Entry" ));
}
LDAPEntry resEntry(i->second);
i++;
LDAPAttribute curAttr(i->first);
LDAPAttributeList *curAl = new LDAPAttributeList();
for ( ; i != m_currentRecord.end(); i++ )
{
if ( i->first == curAttr.getName() )
{
curAttr.addValue(i->second);
}
else
{
const LDAPAttribute* existing = curAl->getAttributeByName( i->first );
if ( existing )
{
// Attribute exists already (handle gracefully)
curAl->addAttribute( curAttr );
curAttr = LDAPAttribute( *existing );
curAttr.addValue(i->second);
curAl->delAttribute( i->first );
}
else
{
curAl->addAttribute( curAttr );
curAttr = LDAPAttribute( i->first, i->second );
}
}
}
curAl->addAttribute( curAttr );
resEntry.setAttributes( curAl );
return resEntry;
}
int LdifReader::getLdifLine(std::string &ldifline)
{
DEBUG(LDAP_DEBUG_TRACE, "-> LdifReader::getLdifLine()" << std::endl);
this->m_lineNumber++;
if ( ! getline(m_ldifstream, ldifline) )
{
return -1;
}
while ( m_ldifstream &&
(m_ldifstream.peek() == ' ' || m_ldifstream.peek() == '\t'))
{
std::string cat;
m_ldifstream.ignore();
getline(m_ldifstream, cat);
ldifline += cat;
this->m_lineNumber++;
}
DEBUG(LDAP_DEBUG_TRACE, "<- LdifReader::getLdifLine()" << std::endl);
return 0;
}
void LdifReader::splitLine(
const std::string& line,
std::string &type,
std::string &value) const
{
std::string::size_type pos = line.find(':');
if ( pos == std::string::npos )
{
DEBUG(LDAP_DEBUG_ANY, "Invalid LDIF line. No `:` separator"
<< std::endl );
std::ostringstream err;
err << "Line " << this->m_lineNumber << ": Invalid LDIF line. No `:` separator";
throw( std::runtime_error( err.str() ));
}
type = line.substr(0, pos);
if ( pos == line.size() )
{
// empty value
value = "";
return;
}
pos++;
char delim = line[pos];
if ( delim == ':' || delim == '<' )
{
pos++;
}
for( ; pos < line.size() && isspace(line[pos]); pos++ )
{ /* empty */ }
value = line.substr(pos);
if ( delim == ':' )
{
// Base64 encoded value
DEBUG(LDAP_DEBUG_TRACE, " base64 encoded value" << std::endl );
char outbuf[value.size()];
int rc = sasl_decode64(value.c_str(), value.size(),
outbuf, value.size(), NULL);
if( rc == SASL_OK )
{
value = std::string(outbuf);
}
else if ( rc == SASL_BADPROT )
{
value = "";
DEBUG( LDAP_DEBUG_TRACE, " invalid base64 content" << std::endl );
std::ostringstream err;
err << "Line " << this->m_lineNumber << ": Can't decode Base64 data";
throw( std::runtime_error( err.str() ));
}
else if ( rc == SASL_BUFOVER )
{
value = "";
DEBUG( LDAP_DEBUG_TRACE, " not enough space in output buffer"
<< std::endl );
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": Can't decode Base64 data. Buffer too small";
throw( std::runtime_error( err.str() ));
}
}
else if ( delim == '<' )
{
// URL value
DEBUG(LDAP_DEBUG_TRACE, " url value" << std::endl );
std::ostringstream err;
err << "Line " << this->m_lineNumber
<< ": URLs are currently not supported";
throw( std::runtime_error( err.str() ));
}
else
{
// "normal" value
DEBUG(LDAP_DEBUG_TRACE, " string value" << std::endl );
}
DEBUG(LDAP_DEBUG_TRACE, " Type: <" << type << ">" << std::endl );
DEBUG(LDAP_DEBUG_TRACE, " Value: <" << value << ">" << std::endl );
return;
}
std::string LdifReader::readIncludeLine( const std::string& line ) const
{
std::string::size_type pos = sizeof("file:") - 1;
std::string scheme = line.substr( 0, pos );
std::string file;
// only file:// URLs supported currently
if ( scheme != "file:" )
{
DEBUG( LDAP_DEBUG_TRACE, "unsupported scheme: " << scheme
<< std::endl);
}
else if ( line[pos] == '/' )
{
if ( line[pos+1] == '/' )
{
pos += 2;
}
file = line.substr(pos, std::string::npos);
DEBUG( LDAP_DEBUG_TRACE, "target file: " << file << std::endl);
}
return file;
}
|