OpenLDAP API Search - c++

I'm attempting to do an LDAP search using the OpenLDAP API. I've already successfully connected and bound to the server. I've done the search with ldap_search_ext_s() and parsed the result with ldap_parse_result(). However, I can't seem to figure out how to get the actual results of the search. Unfortunately, the OpenLDAP C API has changed recently and many of the existing examples on the Internet do not use the current API.
I've already attempted to use ldap_first_attribute(), ldap_next_attribute(), and ldap_get_values() as shown on http://www-archive.mozilla.org/directory/csdk-docs/search.htm (Example 6-13). However, it appears that ldap_get_values() is now deprecated and that ldap_get_values_len() is the closest replacement. Instead of returning a char**, the new function returns a berval**. I've attempted to tweak this example code by creating a berval* with the value of barval**[i]. This results in a successful compile, but a core dump at ber_scanf().
Does anyone know how to get the results of an LDAP search with the OpenLDAP C API?
UPDATE:
In particular, I'm asking how to get the attributes requested from the search message.

The result of a search request always contains a series of SearchResultEntry or SeachResultReference messages, this series terminated by a SearchResultDone message. Calling getNextAttribute (in any language and in any API) makes no sense whatever because the search results are a list of messages. An API should package the array of entries or references in such a way wherein the caller can simply retrieve the list of entries or references. Look for a method that does that.

After taking a look at the OpenLDAP API source code and seeing how the berval value was used, I eventually stumbled upon how to get it's value.
First, you have to get the first entry with ldap_first_entry(). Then, you need to get the first attribute in that entry with ldap_first_attribute(). Then, put the values in a berval** array with ldap_get_values_len(). The returned attribute values can then be access with berval[i]->bv_val.
You can get the next entries and attributes with ldap_next_entry() and ldap_next_attribute(), respectively.
I hope this helps anyone who has a similar issue.

hope the bellow function may help you,
int ldap_search_result(LDAP *ld, char *search_filter, char *search_base)
{
LDAPMessage *result;
BerElement *ber;
char *attr;
char **val;
if(ldap_search_ext_s(ld, search_base, LDAP_SCOPE_CHILDREN,
search_filter, NULL, 0, NULL, NULL, NULL, -1, &result) != LDAP_SUCCESS) {
return -1;
}
if(ldap_count_entries(ld,result) != 1) { // assuming search_filter is unique,
// matches only one entry, and so
// search routine returns only one entry
return -1;
}
if((attr = ldap_first_attribute(ldp, result, &ber)) == NULL) {
return -1;
}
do {
if((val = ldap_get_values(ldp,result,attr)) == NULL) {
return -1;
}
printf(" %s : %s \n", attr, val[0]); // assuming all attributes are single -
//valued.
ldap_memfree(attr);
ldap_value_free(val);
while((attr = ldap_next_attribute(ld,result,ber)) != NULL);
return 0;
}

Related

Get query from sqlite3_stmt of SQLite in c/cpp [duplicate]

I'm using SQLite (3.6.4) from a C++ application (using the standard C api). My question is: once a query has been prepared, using sqlite3_prepare_v2(), and bound with parameters using sqlite3_bind_xyz() - is there any way to get a string containing the original SQL query?
The reason is when something goes wrong, I'd like to print the query (for debugging - this is an in-house developer only test app).
Example:
sqlite3_prepare_v2(db, "SELECT * FROM xyz WHERE something = ? AND somethingelse = ?", -1, &myQuery, NULL);
sqlite3_bind_text(myQuery, 1, mySomething);
sqlite3_bind_text(myQuery, 2, mySomethingElse);
// ....
// somewhere else, in another function perhaps
if (sqlite3_step(myQuery) != SQLITE_OK)
{
// Here i'd like to print the actual query that failed - but I
// only have the myQuery variable
exit(-1);
}
Bonus points if it could also print out the actual parameters that was bound. :)
You probably want to use sqlite3_trace
This will call a callback function (that you define) and on of the parameters is a char * of the SQL of the prepared statements (including bound parameters).
As per the comments in sqlite3.c (amalgamation), sqlite3_sql(myQuery) will return the original SQL text.
I don't see any function for finding the value bound at a particular index, but we can easily add one to the standard set of SQLite functions. It may look something like this:
const char* sqlite3_bound_value(sqlite3_stmt* pStmt, int index)
{
Vdbe *p = (Vdbe *)pStmt;
// check if &p->aVar[index - 1] points to a valid location.
return (char*)sqlite3ValueText(&p->aVar[index - 1], SQLITE_UTF8);
}
Well, the above code shows only a possible way sqlite3_bound_value() could be implemented. I haven't tested it, it might be wrong, but it gives certain hints on how/where to start.
Quoting the documentation:
In the "v2" interfaces, the prepared statement that is returned (the sqlite_stmt object) contains a copy of the original SQL text.
http://www.sqlite.org/c3ref/prepare.html

Unable to set user_version in SQLite

I am trying to use the user_version feature of an SQLite database. I am unable to actually set the user_version and I am not sure why. I have tried various ways of executing a query to update the user_version, and have searched extensively online and am now at a total loss.
Here is my query string.
const std::string kSetUserVersion = "PRAGMA user_version = 1;";
And here is where I am trying to set the user_version, however my result is still 0.
// Set the user version and check it is correct.
sqlite3_exec(dbConnection->db_handle, kSetUserVersion.c_str(), nullptr, nullptr, &db_err); // <-- this is not working!
long currentVersion = sqlite3_exec(dbConnection->db_handle, kGetUserVersion.c_str(), nullptr, nullptr, &db_err);
// If the PRAGMA statement fails, no error returns and it fails silently, so having to add check to see if it has worked.
if (currentVersion != 1) // Setting the user_version has failed.
{
throw DatabaseAccessException(sqlite3_errmsg(dbConnection->db_handle));
}
Any help is much appreciated.
sqlite3_exec() return value is a status/error code and not the value from your query.
To get the query result value, there are two primary options:
Use sqlite3_prepare_v2(), sqlite_step() for SQLITE_ROW and access the data with e.g. sqlite3_column_int(), finalizing the prepared query with sqlite3_finalize().
Supply a callback function as the third argument to your sqlite3_exec() to capture the value. See e.g. here for example: Proper use of callback function of sqlite3 in C++

extract contacts and distribution lists (groups) from wab (mapi)

I'm looking a way to extract all contacts and distribution list (with related contacts) from a WAB (Windows Address Book).
I need to do this because I need to import address books, with distribution lists/groups, in roundcube.
After some research, I've founded a C++ project on Code Project ( http://www.codeproject.com/Articles/3407/Accessing-the-Windows-Address-Book ), but this support only contacts.
Looking around on MSDN I've founded that the OpenEntry method of iAddrBook should support in the third parameter the interface to open, iDistList in my case, but I can't find anywhere the interface id.
I searched some documentation abount WAB file structure, but nothing.
Any suggestions?
Here is a path I used to acomplish this:
I use WABOpen method from wab32 dll to get LPADRBOOK lpAddr;
I set PAB folder lpAddr: call lpAddr->GetPAB(), OpenEntry to get container, then GetContentsTable on container to get LPMAPITABLE lpTable.
To get table element count use GetRowCount on lpTable
To verify if given table row is a distribution list, use QueryRows, this should return MAPI_DISTLIST or MAPI_MAILUSER.
If this is a MAPI_DISTLIST row then use its PR_ENTRYID to call lpAdrBook->OpenEntry(), this should return IABContainer container on which you call GetContentsTable to get table with contents of this distribution list.
the last OpenEntry looks as follows, I do not set third parameter anywhere:
// Now emails will be loaded.
ULONG ulObjType;
IUnknown* lpUnk;
hr = lpAdrBook->OpenEntry(DistEntryId.size, (LPENTRYID)DistEntryId.ab, NULL, 0, &ulObjType, &lpUnk );
if (FAILED(hr)) {
assert(false); return hr;
}

Get original SQL query from prepared statement in SQLite

I'm using SQLite (3.6.4) from a C++ application (using the standard C api). My question is: once a query has been prepared, using sqlite3_prepare_v2(), and bound with parameters using sqlite3_bind_xyz() - is there any way to get a string containing the original SQL query?
The reason is when something goes wrong, I'd like to print the query (for debugging - this is an in-house developer only test app).
Example:
sqlite3_prepare_v2(db, "SELECT * FROM xyz WHERE something = ? AND somethingelse = ?", -1, &myQuery, NULL);
sqlite3_bind_text(myQuery, 1, mySomething);
sqlite3_bind_text(myQuery, 2, mySomethingElse);
// ....
// somewhere else, in another function perhaps
if (sqlite3_step(myQuery) != SQLITE_OK)
{
// Here i'd like to print the actual query that failed - but I
// only have the myQuery variable
exit(-1);
}
Bonus points if it could also print out the actual parameters that was bound. :)
You probably want to use sqlite3_trace
This will call a callback function (that you define) and on of the parameters is a char * of the SQL of the prepared statements (including bound parameters).
As per the comments in sqlite3.c (amalgamation), sqlite3_sql(myQuery) will return the original SQL text.
I don't see any function for finding the value bound at a particular index, but we can easily add one to the standard set of SQLite functions. It may look something like this:
const char* sqlite3_bound_value(sqlite3_stmt* pStmt, int index)
{
Vdbe *p = (Vdbe *)pStmt;
// check if &p->aVar[index - 1] points to a valid location.
return (char*)sqlite3ValueText(&p->aVar[index - 1], SQLITE_UTF8);
}
Well, the above code shows only a possible way sqlite3_bound_value() could be implemented. I haven't tested it, it might be wrong, but it gives certain hints on how/where to start.
Quoting the documentation:
In the "v2" interfaces, the prepared statement that is returned (the sqlite_stmt object) contains a copy of the original SQL text.
http://www.sqlite.org/c3ref/prepare.html

Use c++ to access internet explorer

Well as the topic says, I want to know if there is a tool or a tutorial that can help me access IE, get in a certain URL, do some action on that website. So I would have a program to do that for me instead of doing it myself every time.
Here is a project on Internet Explorer automation with C++
you should really rephrase your question.. you said what you want to do is login to hotmail programatically, check the pidgin code, they do it.
Documentation found here , here and you can I think navigate through the code and tutorials at will until you have your understanding of how the pidgin contributors did it.
You can find the main page for pidgin here
Code sample to get you started:
00362 static void
00363 msn_show_hotmail_inbox(PurplePluginAction *action)
00364 {
00365 PurpleConnection *gc;
00366 MsnSession *session;
00367
00368 gc = (PurpleConnection *) action->context;
00369 session = gc->proto_data;
00370
00371 if (session->passport_info.file == NULL)
00372 {
00373 purple_notify_error(gc, NULL,
00374 _("This Hotmail account may not be active."), NULL);
00375 return;
00376 }
00377
00378 purple_notify_uri(gc, session->passport_info.file);
00379 }
00652 void *
00653 purple_notify_uri(void *handle, const char *uri)
00654 {
00655 PurpleNotifyUiOps *ops;
00656
00657 g_return_val_if_fail(uri != NULL, NULL);
00658
00659 ops = purple_notify_get_ui_ops();
00660
00661 if (ops != NULL && ops->notify_uri != NULL) {
00662
00663 void *ui_handle = ops->notify_uri(uri);
00664
00665 if (ui_handle != NULL) {
00666
00667 PurpleNotifyInfo *info = g_new0(PurpleNotifyInfo, 1);
00668 info->type = PURPLE_NOTIFY_URI;
00669 info->handle = handle;
00670 info->ui_handle = ui_handle;
00671
00672 handles = g_list_append(handles, info);
00673
00674 return info->ui_handle;
00675 }
00676 }
00677
00678 return NULL;
00679 }
Rather than using IE for such things, look into appropriate screen scraping libraries for your language of choice. You can google and search Stack Overflow to find many such libraries. From here, you'll use your language's web APIs to send data to the server.
Don't know of any tool.
I use an embedded browser for such things. It is possible to connect to a running instance of IE. See
Connect to Running Instance of IE
Once you get an instance of IWebBrowser2, the coding is the same.
1. Get the Document Interface
pWebBrowser->Document->QueryInterface(
IID_IHTMLDocument2,(LPVOID*)&Doc);
2. Get all the elements on the Document
Doc->get_all(&Elements);
3. enum the Elements
Elements->get_length(&ulLen);
for_each
Elements->item(item, index, &ppvElement);
4. Detemine what element is desired.
* by classname
* by ID etc.. here I used the classname
ppvElement->get_className (&bstrElement);
5. Insert Text for user / password
ppvElement->put_innerText(wsUreser_or_Psswd)
6. Find the Sign in button and click it.
ppvElement->Click();
Your results may vary.
--
Michael
Why don't you make a feed in dapper in two minutes? Apparently some people have already done it as well.