Skip to content

Commit

Permalink
- added my old project to Git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Maqentaer authored and DeskAlerts committed Nov 27, 2013
1 parent e5d7ce7 commit e592c9a
Show file tree
Hide file tree
Showing 28 changed files with 3,117 additions and 16 deletions.
129 changes: 129 additions & 0 deletions LDAPQuery/AssocObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright (c) 2013, Roman Glebsky (Maqentaer)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

#include "StdAfx.h"
#include "AssocObject.h"

CAssocObject::CAssocObject(void): m_id(DISPID_VALUE + 1L) //start from non zero
{
}

CAssocObject::~CAssocObject(void)
{
}

void CAssocObject::AddValue(CString name, CComVariant value)
{
idValue.insert(std::pair<int,CComVariant>(m_id, value));
nameId.insert(std::pair<CString,int>(name,m_id));
++m_id;
}

STDMETHODIMP CAssocObject::GetIDsOfNames(
/* [in] */ REFIID /*riid*/,
/* [size_is][in] */ LPOLESTR *rgszNames,
/* [in] */ UINT /*cNames*/,
/* [in] */ LCID /*lcid*/,
/* [size_is][out] */ DISPID *rgDispId )
{
std::map<CString,DISPID>::const_iterator it = nameId.find(CString(*rgszNames));
if (it!=nameId.end())
{
*rgDispId = (*it).second;
}
else
{
*rgDispId = m_id++;
nameId.insert(std::pair<CString,int>(*rgszNames,*rgDispId));
}
return S_OK;
}

STDMETHODIMP CAssocObject::Invoke(
/* [in] */ DISPID dispIdMember,
/* [in] */ REFIID /*riid*/,
/* [in] */ LCID /*lcid*/,
/* [in] */ WORD wFlags,
/* [out][in] */ DISPPARAMS *pDispParams,
/* [out] */ VARIANT *pVarResult,
/* [out] */ EXCEPINFO* /*pExcepInfo*/,
/* [out] */ UINT* /*puArgErr*/)
{
if(pDispParams && pDispParams->cArgs > 0)
{
CString name;
CComVariant var0 = pDispParams->rgvarg[pDispParams->cArgs-1];
if(var0.vt == (VT_VARIANT | VT_BYREF))
var0 = *var0.pvarVal;
if(var0.vt == VT_BSTR)
{
name = var0.bstrVal;
}
else if(var0.vt == (VT_BSTR | VT_BYREF))
{
name = *var0.pbstrVal;
}
if(!name.IsEmpty())
{
std::map<CString,DISPID>::const_iterator it = nameId.find(name);
if (it!=nameId.end())
{
dispIdMember = (*it).second;
}
else
{
dispIdMember = m_id++;
nameId.insert(std::pair<CString,int>(name,dispIdMember));
}
}
}
if(wFlags & DISPATCH_PROPERTYGET || wFlags == DISPATCH_METHOD)
{
CComVariant result;
std::map<DISPID,CComVariant>::const_iterator itm = idValue.find(dispIdMember);
if (itm!=idValue.end())
{
result = (*itm).second;
}
result.Detach(pVarResult);
}
else if(wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF)
{
std::map<DISPID,CComVariant>::iterator itm = idValue.find(dispIdMember);
if (itm!=idValue.end())
{
(*itm).second = pDispParams->rgvarg[0];
}
else if(dispIdMember > DISPID_VALUE)
{
idValue.insert(std::pair<int,CComVariant>(dispIdMember, pDispParams->rgvarg[0]));
}
}
return S_OK;
}
79 changes: 79 additions & 0 deletions LDAPQuery/AssocObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright (c) 2013, Roman Glebsky (Maqentaer)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

#pragma once

#include <map>
#include <vector>
#include <atlstr.h>
#include "LDAPQueryLib_i.h"

class CAssocObject :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CAssocObject>,
public IDispatchImpl<IDispatch, &IID_IDispatch, &LIBID_LDAPQueryLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
CAssocObject(void);
~CAssocObject(void);

DECLARE_PROTECT_FINAL_CONSTRUCT()
DECLARE_NO_REGISTRY()

BEGIN_COM_MAP(CAssocObject)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()

private:
std::map<DISPID,CComVariant> idValue;
std::map<CString,DISPID> nameId;
DISPID m_id;

public:
void AddValue(CString name, CComVariant value);

//IDispatch
STDMETHOD(GetIDsOfNames)(
/* [in] */ REFIID riid,
/* [size_is][in] */ LPOLESTR *rgszNames,
/* [in] */ UINT cNames,
/* [in] */ LCID lcid,
/* [size_is][out] */ DISPID *rgDispId);

STDMETHOD(Invoke)(
/* [in] */ DISPID dispIdMember,
/* [in] */ REFIID riid,
/* [in] */ LCID lcid,
/* [in] */ WORD wFlags,
/* [out][in] */ DISPPARAMS *pDispParams,
/* [out] */ VARIANT *pVarResult,
/* [out] */ EXCEPINFO *pExcepInfo,
/* [out] */ UINT *puArgErr);

};
174 changes: 174 additions & 0 deletions LDAPQuery/AttributesSchema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
Copyright (c) 2013, Roman Glebsky (Maqentaer)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

#include "stdafx.h"

#include "DataTypes.h"
#include "AttributesSchema.h"

CAttributesSchema::CAttributesSchema(LDAP *ld, PTCHAR schema)
{
CSimpleArray<PTCHAR> a;
a.Add(_T("attributeTypes"));
a.Add(NULL);
LDAPMessage *pMsg = NULL;
if(ldap_search_s(ld, schema, LDAP_SCOPE_BASE, _T("(objectClass=*)"), a.GetData(), 0, &pMsg) == LDAP_SUCCESS)
{
LDAPMessage* const entry = ldap_first_entry(ld, pMsg);
if(entry)
{
PTCHAR attr;
BerElement* berElem = NULL;
for(attr = ldap_first_attribute(ld, entry, &berElem); attr != NULL; attr=ldap_next_attribute(ld, entry, berElem))
{
PTCHAR *vals;
if((vals = ldap_get_values(ld, entry, attr)) != NULL)
{
for(ULONG i=0UL; vals[i]!=NULL; i++)
{
PTCHAR start = cs_schema_name(vals[i], _T("SYNTAX")), end;
if(start != NULL)
{
start += 6;
end = cs_schema_value(start); //get value
const size_t len = end?end-start:wcslen(start); //length of value
CString type(start, static_cast<int>(len)); //copy value
AttrSchema attr;
attr.type = getAttributeType(type);
attr.single = (cs_schema_name(start, _T("SINGLE-VALUE")) != NULL);

start = cs_schema_name(vals[i], _T("NAME"));
if(start != NULL)
{
start += 4;
while(*start == _T(' ')) start++; //skip spaces
PTCHAR close = (*start == _T('('))?_tcschr(++start, _T(')')):NULL; //it is collection of names. find the end of it
do
{
end = cs_schema_value(start); //get value
const size_t len = end?end-start:close?close-start:_tcslen(start); //length of value
if(!len) break; //not found
CString name(start, static_cast<int>(len)); //copy value
_attrSchemaMap.SetAt(name, attr);
start += len; //move start pointer to end of value
if(*start == _T('\'') || *start == _T('"')) start++; //skip quote
while(*start == _T(' ')) start++; //skip spaces
}
while(start < close); //if it was collection - repeat to the end of collection
}
}
}
ldap_value_free(vals);
}
ldap_memfree(attr);
}
}
ldap_msgfree(pMsg);
}
}

const AttrSchema *CAttributesSchema::Lookup(const CString &attr)
{
AttrSchema *result = NULL;
CAtlMap<CString, AttrSchema>::CPair *pair = _attrSchemaMap.Lookup(attr);
if(pair)
{
result = &pair->m_value;
}
return result;
}

/*static*/ CComVariant CAttributesSchema::getAttributeVariant(PBERVAL attr, const DataType type)
{
CComVariant result;
if(type == OctetString) //we need to convert only binary data
{
CComSafeArray<BYTE, VT_UI1> resArray(attr->bv_len);
for(ULONG i=0; i < attr->bv_len; i++)
{
const BYTE val = (attr->bv_val[i]&0xFF);
resArray.Add(sizeof(BYTE), &val, false);
}
result = resArray;
}
return result;
}

/*static*/ DataType CAttributesSchema::getAttributeType(const CString &type)
{
DataType result;
if(type == _T("1.3.6.1.4.1.1466.115.121.1.40")) //we need to convert only binary data
result = OctetString;
else
result = DirectoryString;
return result;
}

/*static*/ bool CAttributesSchema::isWordChar(const TCHAR ch)
{
return ((ch >= _T('a') && ch <= _T('z')) || (ch >= _T('A') && ch <= _T('Z')) || (ch >= _T('0') && ch <= _T('9')));
}

/*static*/ PTCHAR CAttributesSchema::cs_schema_name(PTCHAR const str, PTCHAR const name)
{
PTCHAR cp = str;
PTCHAR s1, s2;

if (!*name)
return str;

while (*cp)
{
if(*cp == _T('\'') || *cp == _T('"')) //skip values
{
cp = _tcschr(cp+1, *cp);
if(!cp) break;
cp++;
continue;
}

s1 = cp;
s2 = name;

while ( *s1 && *s2 && !(*s1-*s2) ) //compare with the name
s1++, s2++;

if (!*s2 && (!*s1 || !isWordChar(*s1)) && (str == cp || !isWordChar(*(cp-1)))) //checking if found and it's isolated
return(cp);

cp++;
}
return(NULL);
}

/*static*/ PTCHAR CAttributesSchema::cs_schema_value(PTCHAR &start)
{
while(*start == _T(' ')) start++;
return (*start == _T('\'') || *start == _T('"')) ? _tcschr(++start, *(start-1)) : _tcspbrk(start, _T(" {}()"));
}
Loading

0 comments on commit e592c9a

Please sign in to comment.