Hi all, I’m searching an irule that would direct all the authenticated users (that belong to a specific group defined on the ldap profile/object) to a specific pool. All the others users (that aren’t on that group) have to be redirect to a secondary pool. I’ve found a lot of more complicated ladp irule but nothing for this scenario…I’m not black belt on writing irule and any help would be appreciated, Best regards, Mauro
I wouldn’t recommend trying to write an LDAP query iRule. It’s certainly possible, but it’s binary and very complex. Your best bet, in my opinion, is to have a look at the Access Policy Manager (APM) module. It’ll do what you’re asking for very easily and with minimal (if any) iRule coding.
The LDAP auth profile is a remnant of the Advanced Client Authentication (ACA) module, and will soon disappear (reportedly post-v11). It’d also be a very complicated iRule to do what you’re asking for, because the ACA LDAP auth iRule is designed specifically for auth and not query. Like I said, you can technically build an LDAP query/proxy with iRules, but you’d probably need an iRules black belt on staff to maintain it. So I think you have three primary options:
-
Investigate upgrading to a platform that supports three modules.
-
Investigate adding another (potentially virtual) LTM/APM layer.
-
Write a service on a local web server that can query LDAP and return the data via HTTP response. With this you can use a sideband call in an iRule to send the request to the web server and get your LDAP data that way.
Hi Kevin,
thank you, you’re very helpful ! I’ll test it soon, but before we need to purchase the APM license for the other (internal) BIG-IP cluster: in the middle, I’m wondering to start this configuration following your previous suggestion:
“Write a service on a local web server that can query LDAP and return the data via HTTP response. With this you can use a sideband call in an iRule to send the request to the web server and get your LDAP data that way”
I’ve already asked to my colleagues to configure an internal web server that will contain all the users that have to be authenticated before accessing this application. So,the client (a mobile device) will contact the VIP on the LTM and it will contact the internal web server using an irule: if the user is on the users’ list, he could access the pool. If not, it won’t access anything and would be dropped.
Do you have an irule similar that I could use as an example? Thank you in advance,
Mauro
Okay, let’s say you have a PHP web server that can make LDAP calls based on a parameter in the query string (ex. “?find=user”). Example:
set_time_limit(30);
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);
$ldapserver = '10.80.0.200';
$ldapuser = 'CN=Administrator,CN=users,DC=mydomain,DC=com';
$ldappass = 'password';
$ldaptree = "CN=Users,DC=mydomain,DC=com";
if(!isset($_REQUEST["find"])) {
echo "No query parameter";
} else {
// query string parameter to use in search
$findthis = $_REQUEST["find"];
// what value(s) to return
$justthese = array("userPrincipalName");
$ldapconn = ldap_connect($ldapserver) or die("Could not connect to LDAP server.");
if($ldapconn) {
$ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass) or die ("Error trying to bind: ".ldap_error($ldapconn));
if ($ldapbind) {
$result = ldap_search($ldapconn,$ldaptree, "(sAMAccountName=$findthis)", $justthese) or die ("Error in search query: ".ldap_error($ldapconn));
$data = ldap_get_entries($ldapconn, $result);
if($data["count"] > 0) {
echo $data[0]["userprincipalname"][0];
} else {
echo "No data";
}
} else {
echo "LDAP bind failed...";
}
}
ldap_close($ldapconn);
}
** Ref: PHP: ldap_search - Manual
Now, create an internal virtual server to load balance this web service, and apply this iRule to your application virtual server to call the web service (via sideband with a query parameter).
when RULE_INIT {
user-defined: name of internal web service virtual server
set static::WSVIP "webservice-vs"
user-defined: debug enable/disable (1/0)
set static::DEBUG 1
}
when HTTP_REQUEST {
Prepare the sideband call
set conn [connect -timeout 3000 -idle 30 -status conn_status $static::WSVIP]
if { $static::DEBUG } { log local0. "conn_status = $conn_status" }
if { $conn eq "" } {
if { $static::DEBUG } { log local0. "Sideband connection could not be established" }
return
}
Prepare user information to transmit to APM sideband call
set userdata "bob.user"
Create the data to send to the APM
set data "GET /ldaplookup.php?find=$userdata HTTP/1.1\r\nHost: [HTTP::host]\r\nUser-Agent: cUrl\r\nAccept: */*\r\n\r\n"
if { $static::DEBUG } { log local0. "data = $data" }
Send the sideband call
set send_info [send -timeout 3000 -status send_status $conn $data]
if { $static::DEBUG } { log local0. "send_status = $send_status" }
Receive the APM response (via data "peek")
set start [clock clicks -milliseconds]
for {set i 0} {$i <= $static::retries} {incr i} {
set recv_data [recv -peek -status peek_status -timeout 10 $conn]
if { [string match "HTTP/*\r\n\r\n*" $recv_data] } {
if { [string match -nocase "*Content-Length: *" $recv_data] }{
set header_length [expr {[string first "\r\n\r\n" $recv_data] + 4}]
set payload_length [findstr [string tolower $recv_data] "content-length: " 16 "\r"]
if { $payload_length ne "" and $payload_length > 0 } {
set recv_data [recv -peek -timeout 3000 -status recv_status [expr {$header_length + $payload_length}] $conn]
break
} else {
break
}
} else {
break
}
}
}
set returned_data [findstr $recv_data "\r\n\r\n" 4]
if { $static::DEBUG } { log local0. "recv_data = $recv_data" }
if { $static::DEBUG } { log local0. "filtered data = $returned_data" }
Close the connection
close $conn
The data returned from the web server is now in the $returned_data variable...
...do something here...
}
The send string data now contains a query string instead of an arbitrary header. This could be done either way though.
Prepare user information to transmit to APM sideband call
set userdata "bob.user"
Create the data to send to the APM
set data "GET /ldaplookup.php?find=$userdata HTTP/1.1\r\nHost: [HTTP::host]\r\nUser-Agent: cUrl\r\nAccept: */*\r\n\r\n"
And then the returned sideband call LDAP data can be found in the $recv_data or $returned_data variables. What you do with that data after this is up to you. Also notice that the web service PHP will return “No data” if the LDAP query doesn’t find anything. The $returned_data variable would actually contain this error message, so you could very simply issue a reject based on this value.
if { $returned_data equals "No data" } {
log local0. "No data found - rejecting user"
reject
}
If you’re talking about a query that entails a simple “if exists” check, or at best a single value lookup, then a data group of 1000 or more entries is both fast and efficient. So depending on what you’re querying on in the request, the iRule would look something like this:
when HTTP_REQUEST {
if { [class match $value_from_uri equals my_datagroup] } {
do something here
}
}
“$value_from_uri” is an arbitrary variable I used to illustrate the syntax. I’m not sure exactly what you’d be searching the LDAP or web server for, or where in the request payload you’d find it. The point is that you’re going to presumably extract some value from the request and then look for that value in data group. Here’s another example to illustrate that:
when HTTP_REQUEST {
if { [class match [string tolower [HTTP::uri]] starts_with my_datagroup] } {
do something
}
}
Here I’m searching the data group for the beginning of the request URI. Try to think about what you were going to query the LDAP server for (a user name perhaps?), what you expected to receive from the query (a list of URIs they could access? or just existence?), and where you were going to get the information from to make the request (a logon form?).
So this value is defined by the client? Are you authenticating the user beyond this query string value?
To get this query string value, you can use the URI::query command. Example:
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] contains "microsoft-server-activeSync?cmd" } {
set user [URI::query [HTTP::uri] User]
if { [class match $user my_datagroup] } {
User in data group. Allow access?
}
}
}
By the way, is this user value in every request, or do we need to set and track a session token beyond this (initial?) request?
Hi kevin, finaly, as you’ve suggested, I’ve activated the APM module on another (virtual) appliance. But this is my first time working with the APM module and I’m having some problems:
This is the task list that you’ve suggested:
Provision APM on the separate box. OK, done
Create an APM LDAP AAA object that sets up the authentication to the LDAP server. OK. Do I need administrator right for the user?
Create an access policy that includes a simply LDAP query KO
I don’t find on the APM menu where I can configure this access policy
Thanks in advance for your help,
Mauro
I’ve found that menu and I’ve created the AD auth and resources macro. But If I try to edit the “AD auth” object, I can’t find the AAA ldap server previusly created ![]()
Create an APM LDAP AAA object that sets up the authentication to the LDAP server. OK. Do I need administrator right for the user?
It’s an LDAP query, so you just need to specify an account that has the rights to make such a query.
Create an access policy that includes a simply LDAP query KO
I don’t find on the APM menu where I can configure this access policy
Once you’ve created the access policy, open it up, go to the third tab (Access Policy), and click the “Edit Access Policy for Profile” link. This will bring up a new window with the access profile’s “visual policy”. Inside the visual policy, create an LDAP Query agent, specify the previously-created LDAP AAA, and then the following properties:
-
SearchDN: the distinguished name path where the users can be found (ex. cn=users,dc=mydomain,dc=com)
-
SearchFilter: this is what you’re looking for. The syntax would be something like:
attribute-name=%{session.value}where “attribute-name” is the literal name of the AD/LDAP attribute (ex. userPrincipalName, sAMAccountName, etc.). And “%{session.value}” is a session variable that you’ve already assigned prior to this LDAP query agent (via iRule or Variable Assignment agent).
-
Branch Rules: on the Branch Rules tab, click the “change” link next to the default expression, in the next window, click the “x” to the right of that expression to delete it, click the “Add Expression” button, and select “LDAP Query” and “LDAP Query Passed” from the drop down boxes, and click the “Add Expression” button. Click the “Finished” button, and then optionally change the name field to something more appropriate like “Query passed”. Click Save.
With this in place, the agent will perform the query and follow the “Query passed” branch if the query was successful. Run a quick test, and then run an access policy report in the GUI. If the LDAP query was successful, you’ll see a bunch of session.ldap.last.attr session variables in the session cache. Any of these can be used in evaluations after the LDAP query agent.
I’ve found that menu and I’ve created the AD auth and resources macro. But If I try to edit the “AD auth” object, I can’t find the AAA ldap server previusly created
An AD Auth/Query will only allow an AD AAA object, likewise for an LDAP Auth/Query. You can use either to perform a query, but the LDAP query is generally faster.
Hi Kevin,
ok, thanks.
Tomorrow I’ll test/check what you’ve suggested. How can I test (locally on the APM/LTM, before starting to work with the irule) if the query will work as desired? If the query will work, the next step will be (as you’ve already suggested in previous messages) configuring the irule that will extract the user from the http request and pass the information to the apm module…I’m also wondering to simplify the scenario using this virtual appliance both for the query and the pool management. The frontend device (ASM) will only check for any signatures/anomaly and will pass all the requests to the VIP on the BE where I have both the APM and the LTM.
Mauro
Hi Kevin, as you’ve suggested, I’ve configured the access policy and the branch rule. Now, as I’ve written, I’d like to use the same appliance both as a LTM and APM module. Do I need the same irule that you’ve had previously suggested (that one that you’ve suggestd for an external LTM) or something more simple? Best regards, Mauro
Hi Kevinn,
I’ve upgraded to the 11.4 hf4 version (I’ve read that in this version “has the ability to apply an Access Policy on top of LTM”) and all the configurations for the access policy and the LDAP server were implemented. If I try to access the VIP, the session could not be established, but I see that the BIG-IP is sending something to the LDAP server.
Now I’m wondering that only the irule for catching the users form the query is missing. The user (I’ve done e new log session for trace how these requests are coming) in a GET request, on the URI, something like: /Microsoft-Server-ActiveSync?User=Pippo&Device=App
I’m wondering that (using both APM and LTM on the same appliance and the 11.4 version) this irule will be something more simple…any suggestions for this irule?
thanks in advance for your precious support,
Mauro
Do I need the same irule that you’ve had previously suggested (that one that you’ve suggestd for an external LTM) or something more simple?
First, if you’re doing APM on the load balancing appliance, the configuration is actually much simpler than I’ve described, and probably won’t require much iRule code at all. Your APM access (visual) policy should, more or less, look like the following:
start - iRule event - LDAP Query - Allow
Where the iRule event is used to the extract any values necessary from the client request, the LDAP query (with an attached LDAP AAA) performs an LDAP query with the values provided, and then you either allow the traffic based on the success or failure of the LDAP query, or add additional conditions to the policy. The only iRule code you may need will be in pulling the necessary value(s) from the client request.
How can I test (locally on the APM/LTM, before starting to work with the irule) if the query will work as desired?
The easiest way to troubleshoot an APM access is to drop message boxes in the visual policy at different stages so that you can see where it’s going, and potentially see session variables at different points. Add a message box where you want to see the path taken (ex. before and after the LDAP query). To see any session variables that may have been created, use the %{} syntax in the message box text fields. Exampele:
%{session.ldap.last.attr.userPrincipalName}
but the irule is assigned on the virtual server\resources menu. Do I have to assign it using the visual manager?
It shouldn’t matter where it is in the visual policy as long as the flow makes sense. It may be simpler at this point to back out and test the LDAP query by itself. If you can create a NEW access profile, please do so and follow these steps:
-
Create your LDAP AAA
-
Create your new access profile and open the visual policy editor
-
Add a Variable assignment agent as the first new item and create a custom session variable. Example:
session.custom.user = expr { "bob.user" }Substitute the user with some name you know exists in the LDAP directory.
-
After the Variable assignment, create your LDAP Query agent.
-
AAA server - the LDAP AAA server you just created
-
SearchDN - the DN path where users can be found.
-
SearchFilter - this is the LDAP object you’re looking for, based on the previously-created custom variable. Example:
userPrincipalName=%{session.custom.user}
-
-
Go to the Branch tab of the LDAP Query agent.
- Change the Name field to something like “Query passed”
- Click the Change link
- Click the x in the top right corner to delete the existing expression
- Click the Add Expression button
- Agent Sel should be “LDAP Query”
- Condition should be “LDAP Query Passed”
- Click the Add Expression button
- Click Finished
- Click Save
Set the ending block after the Query passed branch to Allow and test this access policy attached to a VIP. The idea here is that you’ve short circuited the process to focus on the LDAP query itself. If the query works with the static user value from the custom variable, you should be able to run an access policy report and see all of the LDAP values returned. You then just need to:
- Modify how the custom variable is set - presumably from HTTP request URI data
- Apply this same LDAP query configuration to your original access profile
Hi Kevin,
ok,thanks.
tomorrow I’ll test it as you’ve suggested,
Mauro
Hi Kevin,
here I’ve tested the query using the variable assignment, but this test has failed.
I’ve checked the attributes on the LDAP server using an LDAP search program and the problem is that this users are on that grup as a members and with all the complete DN instead of the sAMAccountName . So, the sAMAccountName is the value of the user in that DB, but this isn’t the attributes that I’ve to search with the query… Checking the apm logs, I’ve also found thi message:
modules/Authentication/Ldap/LdapAgent.cpp func: “getLdapUserInput()” line: 735 Msg: 60c9f1c8: LDAP Agent: getLdapUserInput(): unable to decrypt user password due to NULL ciphertext
Any ideas?
Thanks in advance,
Mauro
