Paged ldap_search in OpenLDAP to get around size limit? - c

We are currently in the process of migrating from an aged proprietary directory service to OpenLDAP.
Today we ran into the problem that ldap_search_ext_s or ldapsearch in general does not return any results, if the number of entries, which were to be returned by the current search, would hit a certain limit.
Unfortunately setting the size limit higher in the LDAP server configuration might just postpone the problem, as we have a really big database and our update mechanism, which runs every morning, has to performe huge queries.
In the MSDN documentation I noticed that there is a mechanism to perform a paged search, which would allow me to get around the size limitation.
Apparently this is also specified in an RFC draft from 1996 but hasn't been finalized (yet)?
Anyway, since I'm not working on a Windows-Box I have to use the OpenLDAP API, which doesn't seem to provide that mechanism (at least I couldn't find it on their search page)
Which brings me to my question: Do you have an idea what I could do, to solve that problem in an elegant manner?
Thanks for your help!

OpenLDAP supports paged result retrieval via ldap_create_page_control () and friends. Here is a description and sample code. If that doesn't help I may be able to provide excerpts from production code.

I had an issue using ldap_create_page_control with ldap_search_ext_s, my ldap library implementation was using LDAP version 2 by default and it looks it's supported for version 3+. It was returning "Not supported" from ldap_search_ext_s() before I set LDAP to version 3.

I was able to get around the size limitation using ldap_control_paged_result
ldap_control_paged_result is used to Enable LDAP pagination by sending the pagination control. The below function worked perfectly in my case.
function retrieves_users($conn)
{
$dn = 'ou=,dc=,dc=';
$filter = "(&(objectClass=user)(objectCategory=person)(sn=*))";
$justthese = array();
// enable pagination with a page size of 100.
$pageSize = 100;
$cookie = '';
do {
ldap_control_paged_result($conn, $pageSize, true, $cookie);
$result = ldap_search($conn, $dn, $filter, $justthese);
$entries = ldap_get_entries($conn, $result);
if(!empty($entries)){
for ($i = 0; $i < $entries["count"]; $i++) {
$data['usersLdap'][] = array(
'name' => $entries[$i]["cn"][0],
'username' => $entries[$i]["userprincipalname"][0]
);
}
}
ldap_control_paged_result_response($conn, $result, $cookie);
} while($cookie !== null && $cookie != '');
return $data;
}

Use AD or Novell's eDirectory? ;)

Related

Setting Environment-specific database in CakePHP

I am using CakePHP and was trying to implement https://github.com/josegonzalez/cakephp-environments
Which seemed to be going fine except that I have no idea where to specify the env specific database info.
Does anyone know where to set these?
I personally haven't used the plugin, however from looking at the code and the docs, if you were using the suggested database configuration, then it seems that you would define the options as either environment variables, which can be done in various ways, for example
in your server configuration (Apache example)
in your cloud varibale settings (Heroku example)
manually using putenv(), $_ENV, $_SERVER
$name = 'MYSQL_DB_HOST';
$value = 'localhost';
putenv("$name=$value");
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
...
or as CakePHP configuration values via the Environment::configure() calls, something like:
Environment::configure('development',
true,
array(
'MYSQL_DB_HOST' => 'localhost',
'MYSQL_USERNAME' => 'user',
// ...
),
// ...
);

How to determine a Wordpress version remotely?

I have numerous sites and its becoming a nuisance keeping them all up to date, so I would ideally like to compile a list where I can display the version of each website automatically. So I can see at the drop of a hat which ones needs updated and so on.
I have remote access to all off their databases, I had thought about querying the wp_options table for the DB Version but that isn't specific enough when it comes to smaller version updates as far as I am aware.
Any thoughts?
Here's a demo plugin
<?php
/** Plugin Name: My JSON data **/
add_filter( 'query_vars', function( $qv ){
$qv[] = 'mydata';
return $qv;
});
add_action( 'template_redirect', function(){
$input = get_query_var( 'mydata' );
$secret = 'abcdefg'; // Edit this
if( ! empty( $input ) )
{
if( $secret === $input )
{
$data = array(
'version' => $GLOBALS['wp_version'],
'foo' => 'bar',
);
wp_send_json_success( $data );
}
else
{
wp_send_json_error();
}
}
} );
where example.com/?mydata=abcdefg gives
{"success":true,"data":{"version":"3.8.1","foo":"bar"}}
and example.com/?mydata=wrong shows:
{"success":false}
I wouldn't recommend trying to bridge a system to check WordPress, espiecally since the WordPress core since 3.7.1 comes with this functionality.
WordPress 3.7.1+ Auto Updates, so it would be best to upgrade all your WordPress sites - this would also be a great idea for security purposes.
What you might want to consider is removing any redundant plugins and have a plan for updating those plugins every few months too.
3rd-party plugins are usually the reason a site is vulnerable, more so than the core of WordPress. Fight the fire before it becomes a fire in the first place! Use less plugins or keep on top of them.

WCF Data Service and Silverlight: DataServiceQuery<T> will not re-perform query

Using Silverlight 4, Oracle 11g, and Entity Framework 4.
I use a DataServiceQuery to fill a DataGrid. Then, some local (non-EF) code updates the DB. I would like to use the same query to refresh the DataGrid with the updated/new data. The problem is, when I do that, it returns the old, original results. I have verified that the changes have, in fact, been committed to the DB prior to this code running:
DataServiceContext<T> dsContext= new DataServiceContext<T>(uri);
dsContext.MergeOption = MergeOption.NoTracking;
dsContext.SaveChangesDefaultOptions = SaveChangesOptions.ReplaceOnUpdate;
DataServiceQuery<T> dsQuery = dsContext.CreateQuery<T>(typeof(T).Name);
// oldQuery is an IQueryable<T>
dsQuery = (DataServiceQuery<T>)oldQuery;
var dsQuery = (DataServiceQuery<T>)oldQuery;
dsQuery.BeginExecute(new AsyncCallback(c =>
{
IEnumerable<T> result = dsQuery.EndExecute(c);
listSelectedRecord = new List<T>();
listSelectedRecord = result.ToList();
}), dsQuery);
As far as I can tell, the new dsQuery is not even being sent to the Oracle server, even though a new DataServiceContext is being created. It is apparently discovering that it has a cached copy somewhere. If I type the query into a browser, it returns the updated results.
Any suggestions on how to force the DS to reperform the query?
Assigning oldQuery to the newly instantiated dsQuery object apparently copies cached results from somewhere. The solution I used here was to replace
dsQuery = (DataServiceQuery<T>)oldQuery;
with
dsQuery = (DataServiceQuery<T>)(dsQuery.Provider.CreateQuery<T>(oldQuery.Expression));
I would still like to understand exactly where, why, and how these results are being cached. The DataServiceQuery<T> docs don't say anything about this:
http://msdn.microsoft.com/en-us/library/cc646574(v=vs.95).aspx
You can try deleting your .suo file if you see this behavior when debugging.
The astonishing answers to this question seem to be found here:
Does Silverlight cache web service calls?
and
https://connect.microsoft.com/VisualStudio/feedback/details/340931/silverlight-webclient-does-not-download-updated-resources
There seems to be be some sort of vague consensus that SLx does indeed rely on the browser cache, not that you would know that from the DataService, DataServiceContext, or DataServiceQuery docs.
So the easiest fix for this, in IE 8 at least, is to turn off browser caching.

Codeigniter PDO integration

i did lot of research on the web but i didnt find anything that could help me to use PDO in codeigniter. I saw in the change lof of CI 2.1.0(i think) that pdo driver was added to the framwork.
I ended up now with a database.php config file that looks like this:
$db['default']['hostname'] = 'mysql:host=myhostname;port=myport';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydb';
$db['default']['dbdriver'] = 'pdo';
So now(after a lot of wasted time to get the snippet above to work -.- ) i receive no error about connection, but HOW TO EXECUTE QUERY NOW? i cant figure out what syntax will work and how to build queries. Anyone have hints?
PS: if you're wordering about why i need pdo in ci, the answer is my boss want me to create a structured enviroment with:
CI 2.x + (done)
Smarty 3 (done)
PhpUnit (not yet)
PDO (not yet)
so if you have also any hints for integrate phpunit feels free to answer. Ty in advance
You use PDO the same way you use any other database driver in CodeIgniter. If you are still unsure then I would recommend reading the documentation on the Database Class.
You can issue standard queries by explicitly writing the query or you can use the Active Record Class (which is more of a query builder).
Here are some examples:
// Standard query
$results = $this->db->query('SELECT name, title, email FROM my_table');
// Active record
$this->db->select('name, title, email');
$results = $this->db->get('my_table');
As for integrating PHPUnit, have a look at https://github.com/cmillr/CI-PHPUnit (I haven't tested it myself) or look around the CodeIgniter forums. I've seen a ton of topics on integrating PHPUnit with CodeIgniter.
You need to change your config a little:
'dsn' => 'mysql:host=localhost;dbname=codeigniter',
//'hostname' => 'localhost',
'username' => 'codeigniter',
'password' => 'codeigniter',
'database' => 'codeigniter',
Notice we use dsn, not hostname.
After that, simply use your $this->db-> like you always do - the PDO driver will translate everything to PDO methods
A little dated, but the topic is lacking clear explanations & docs so I wrote this - hope it helps clarify for people:
http://codebyjeff.com/blog/2013/03/codeigniter-with-pdo

CakePHP - spitting out XML for webservice

What is the best way to spit out XML for webservice in CakePHP?
I have it like the following but it's displaying an empty page.
Sample call /service/config.xml
In Controller
var $helpers = array('Xml');
function config() {
$this->autoRender = false;
$obj = array("response" => array("config" => array(...)));
$objXmlHelper = new XmlHelper();
$objXml = $objXmlHelper->header();
$objXml .= $objXmlHelper->serilize($obj);
echo $objXml;
}
That gives empty page. However, if I echo json_encode($obj); that actually prints out json.
Thanks,
Tee
You probably have an error in your code. My guess is you are not including the XML helper.
Check you CakePHP (app/tmp/logs/) and PHP logs. In addition you may need to set the DEBUG flag to a higher level ( i.e. > 0).
I'd also recommend considering moving such things to a model. Web Services are typically data access layers and that belongs in the Model.

Resources