2008-03-28 07:05:10 -04:00
|
|
|
// $OpenLDAP$
|
2000-09-01 14:46:32 -04:00
|
|
|
/*
|
2013-01-02 13:20:30 -05:00
|
|
|
* Copyright 2000-2013 The OpenLDAP Foundation, All Rights Reserved.
|
2000-09-01 14:46:32 -04:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO!!!
|
2000-10-03 14:25:34 -04:00
|
|
|
// * some kind of iterator to step through the attribute values
|
|
|
|
|
// * remove values from Attribute
|
2000-09-01 14:46:32 -04:00
|
|
|
// * handling of subtypes (;de; and so on)
|
|
|
|
|
// * some documentation
|
|
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
|
2000-09-01 14:46:32 -04:00
|
|
|
#include <ldap.h>
|
2007-11-07 06:01:03 -05:00
|
|
|
#include <cstdlib>
|
2000-10-03 14:25:34 -04:00
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "StringList.h"
|
|
|
|
|
|
2000-09-01 14:46:32 -04:00
|
|
|
#include "LDAPAttribute.h"
|
|
|
|
|
|
2001-09-28 12:39:58 -04:00
|
|
|
using namespace std;
|
2000-09-01 14:46:32 -04:00
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
LDAPAttribute::LDAPAttribute(){
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute( )" << endl);
|
|
|
|
|
m_name=string();
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-01 14:46:32 -04:00
|
|
|
LDAPAttribute::LDAPAttribute(const LDAPAttribute& attr){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute(&)" << endl);
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
|
|
|
|
|
" attr:" << attr << endl);
|
|
|
|
|
m_name=attr.m_name;
|
|
|
|
|
m_values=StringList(attr.m_values);
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
LDAPAttribute::LDAPAttribute(const string& name, const string& value){
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
|
|
|
|
|
" name:" << name << endl << " value:" << value << endl);
|
|
|
|
|
this->setName(name);
|
|
|
|
|
if(value != ""){
|
|
|
|
|
this->addValue(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LDAPAttribute::LDAPAttribute(const string& name, const StringList& values){
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
|
|
|
|
|
" name:" << name << endl);
|
|
|
|
|
m_name=name;
|
|
|
|
|
m_values=values;
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LDAPAttribute::LDAPAttribute(const char *name, char **values){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
|
|
|
|
|
" name:" << name << endl);
|
2000-09-01 14:46:32 -04:00
|
|
|
this->setName(name);
|
|
|
|
|
this->setValues(values);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LDAPAttribute::LDAPAttribute(const char *name, BerValue **values){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPAttribute::LDAPAttribute()" << endl);
|
|
|
|
|
DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
|
|
|
|
|
" name:" << name << endl);
|
2000-09-01 14:46:32 -04:00
|
|
|
this->setName(name);
|
|
|
|
|
this->setValues(values);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LDAPAttribute::~LDAPAttribute(){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_DESTROY,"LDAPAttribute::~LDAPAttribute()" << endl);
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
void LDAPAttribute::addValue(const string& value){
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
|
|
|
|
|
m_values.add(value);
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LDAPAttribute::addValue(const BerValue *value){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::addValue()" << endl);
|
2000-09-01 14:46:32 -04:00
|
|
|
if(value!=0){
|
2000-10-03 14:25:34 -04:00
|
|
|
this->addValue(string(value->bv_val, value->bv_len));
|
2000-09-01 14:46:32 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LDAPAttribute::setValues(char **values){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
|
|
|
|
|
if(values){
|
|
|
|
|
m_values.clear();
|
|
|
|
|
for( char **i=values; *i!=0; i++){
|
|
|
|
|
this->addValue(*i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LDAPAttribute::setValues(BerValue **values){
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
|
|
|
|
|
if(values){
|
|
|
|
|
m_values.clear();
|
|
|
|
|
for( BerValue **i=values; *i!=0; i++){
|
|
|
|
|
if( this->addValue(*i) ){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-09-01 14:46:32 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
void LDAPAttribute::setValues(const StringList& values){
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::setValues()" << endl);
|
|
|
|
|
m_values=values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StringList& LDAPAttribute::getValues() const{
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getValues()" << endl);
|
|
|
|
|
return m_values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BerValue** LDAPAttribute::getBerValues() const{
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
|
2001-11-14 12:33:54 -05:00
|
|
|
size_t size=m_values.size();
|
2000-10-03 14:25:34 -04:00
|
|
|
if (size == 0){
|
|
|
|
|
return 0;
|
|
|
|
|
}else{
|
2001-11-14 12:33:54 -05:00
|
|
|
BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
|
2000-10-03 14:25:34 -04:00
|
|
|
StringList::const_iterator i;
|
|
|
|
|
int p=0;
|
|
|
|
|
|
|
|
|
|
for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
|
2001-11-14 12:33:54 -05:00
|
|
|
temp[p]=(BerValue*) malloc(sizeof(BerValue));
|
2000-10-03 14:25:34 -04:00
|
|
|
temp[p]->bv_len= i->size();
|
2001-11-14 12:33:54 -05:00
|
|
|
temp[p]->bv_val= (char*) malloc(sizeof(char) * (i->size()+1));
|
2000-10-03 14:25:34 -04:00
|
|
|
i->copy(temp[p]->bv_val,string::npos);
|
|
|
|
|
}
|
|
|
|
|
temp[size]=0;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LDAPAttribute::getNumValues() const{
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getNumValues()" << endl);
|
2000-09-01 14:46:32 -04:00
|
|
|
return m_values.size();
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
const string& LDAPAttribute::getName() const {
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::getName()" << endl);
|
|
|
|
|
return m_name;
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
2000-10-03 14:25:34 -04:00
|
|
|
void LDAPAttribute::setName(const string& name){
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::setName()" << endl);
|
|
|
|
|
DEBUG(LDAP_DEBUG_TRACE | LDAP_DEBUG_PARAMETER," name:" << name << endl);
|
|
|
|
|
m_name.erase();
|
|
|
|
|
m_name=name;
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The bin-FLAG of the mod_op is always set to LDAP_MOD_BVALUES (0x80)
|
|
|
|
|
LDAPMod* LDAPAttribute::toLDAPMod() const {
|
2000-10-03 14:25:34 -04:00
|
|
|
DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::toLDAPMod()" << endl);
|
2001-11-14 12:33:54 -05:00
|
|
|
LDAPMod* ret= (LDAPMod*) malloc(sizeof(LDAPMod));
|
|
|
|
|
ret->mod_op=LDAP_MOD_BVALUES; //always assume binary-Values
|
|
|
|
|
ret->mod_type= (char*) malloc(sizeof(char) * (m_name.size()+1));
|
2000-10-03 14:25:34 -04:00
|
|
|
m_name.copy(ret->mod_type,string::npos);
|
|
|
|
|
ret->mod_type[m_name.size()]=0;
|
2001-11-14 12:33:54 -05:00
|
|
|
ret->mod_bvalues=this->getBerValues();
|
|
|
|
|
return ret;
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LDAPAttribute::isNotPrintable() const {
|
2001-11-14 12:33:54 -05:00
|
|
|
StringList::const_iterator i;
|
|
|
|
|
for(i=m_values.begin(); i!=m_values.end(); i++){
|
|
|
|
|
size_t len = i->size();
|
|
|
|
|
for(size_t j=0; j<len; j++){
|
|
|
|
|
if (! isprint( (i->data())[j] ) ){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
2001-11-14 12:33:54 -05:00
|
|
|
}
|
|
|
|
|
return false;
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ostream& operator << (ostream& s, const LDAPAttribute& attr){
|
2001-11-14 12:33:54 -05:00
|
|
|
s << attr.m_name << "=";
|
|
|
|
|
StringList::const_iterator i;
|
|
|
|
|
if (attr.isNotPrintable()){
|
|
|
|
|
s << "NOT_PRINTABLE" ;
|
|
|
|
|
}else{
|
|
|
|
|
for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
|
|
|
|
|
s << *i << " ";
|
2000-09-01 14:46:32 -04:00
|
|
|
}
|
2001-11-14 12:33:54 -05:00
|
|
|
}
|
2000-09-01 14:46:32 -04:00
|
|
|
return s;
|
|
|
|
|
}
|