php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28291 Can't pass a dynamic variable to $filter in ldap_search()
Submitted: 2004-05-06 01:44 UTC Modified: 2004-05-06 19:15 UTC
From: raphux at raphux dot com Assigned:
Status: Not a bug Package: LDAP related
PHP Version: 4.3.6 OS: GNU/Linux 2.4.26
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: raphux at raphux dot com
New email:
PHP Version: OS:

 

 [2004-05-06 01:44 UTC] raphux at raphux dot com
Description:
------------
I made a script that get the mac address of users on my LAN, and then check in an openldap directory if it exists or not.
The probleme is that it seems to be impossible to pass a dynamic variable to "ldap_search()" for the "filter" field.

Reproduce code:
---------------
<?php
function get_mac(){
        $user_ip = $_SERVER['REMOTE_ADDR'];
        $user_mac = shell_exec('/usr/sbin/arp -a '.$user_ip.' | cut -d" " -f4');
        return $user_mac;
}
$mac = get_mac();               //the client's MAC (on my LAN) is now stored in $mac
$ldap_host = "127.0.0.1";       //my LDAP server
$connect = ldap_connect($ldap_host);                    //get a resource identifier
$base_dn = "ou=utilisateurs,dc=pinel-wifi,dc=org";      //where to search in the directory
$filter = "pager=$mac";
echo $filter;                   //to see if $filter is really what it should be
echo "<br>";
$sr=ldap_search($connect, $base_dn, $filter);           //let's search!
?>


Expected result:
----------------
the value of $filter should be placed correctly in the ldap_search function. In my ldap log, I should have :
May  6 01:47:13 Pinel-WiFi slapd[341]: conn=56 fd=14 ACCEPT from IP=127.0.0.1:32866 (IP=0.0.0.0:389)
May  6 01:47:13 Pinel-WiFi slapd[526]: begin get_filter
May  6 01:47:13 Pinel-WiFi slapd[526]: EQUALITY
May  6 01:47:13 Pinel-WiFi slapd[526]: end get_filter 0
May  6 01:47:13 Pinel-WiFi slapd[526]: conn=56 op=0 SRCH base="ou=utilisateurs,dc=pinel-wifi,dc=org" scope=2 filter="(pager=00:0A:E6:A7:27:CC)"
[...]
I cut the end, uninteresting I think.

I got this log by replacing manually 
$filter = "pager=$mac";
by this :
$filter = "pager=00:0A:E6:A7:27:CC"; 



Actual result:
--------------
And if I leave the original:
$filter = "pager=$mac";

that is what my openldap server recieve :
May  6 01:48:56 Pinel-WiFi slapd[341]: conn=58 fd=14 ACCEPT from IP=127.0.0.1:32868 (IP=0.0.0.0:389)
May  6 01:48:56 Pinel-WiFi slapd[526]: begin get_filter
May  6 01:48:56 Pinel-WiFi slapd[526]: EQUALITY
May  6 01:48:56 Pinel-WiFi slapd[526]: end get_filter 0
May  6 01:48:56 Pinel-WiFi slapd[526]: conn=58 op=0 SRCH base="ou=utilisateurs,dc=pinel-wifi,dc=org" scope=2 filter="(?=undefined)"
[...]
I cut the end too.

As you can see, there is a "(?=undefined)" in the filter field, which, I think, shouldn't be here. I tought that was a bug, so that's why I reported it. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-06 18:57 UTC] raphux at raphux dot com
Ok, I found why it acts like this. Thanks to a friend of mine who has some experiences in php and shell hacking.

The probleme was that the shell_exec() really takes the mac address of the host, but also the \n at the end of the string, even if it doesn't print it out. So I hadded this little function to make it works (thx to zebadger@excite(dhot)com who write a comment on the chop() function)
:

function delbackslash_n($mac){
        $endchar = substr("$mac", strlen("$mac") - 1, 1);
        if ($endchar == "\n")
        {
                $mac = substr("$mac", 0, -1);
                echo '"\n" is present'; // just to verify
                echo "<br>"; //may be commented to :)
        }
        return $mac;
}

It deletes the \n if there is one, and then the ldap-search function works well.

Hop this will be helpfull for someone someday...

Bye!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 18:01:31 2024 UTC