首页 文章

永久ldap认证会话

提问于
浏览
0

我正在尝试构建一个简单的基于Web的目录导航/管理应用程序 .

申请要求:

  • Active Directory(或其他目录服务)域用户访问此Web应用程序并使用相同的域用户/密码凭据登录 .

  • 然后用户可以导航目录树,创建/编辑条目,编辑条目的属性等 .

我正在使用perl Net :: LDAP进行ldap操作,如:

#!/usr/bin/perl -wT

use Net::LDAP;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

my $ssl = 1;
my $srv = '192.168.56.110';
my $uri = $ssl ? 'ldaps://' : 'ldap://';

my $c = Net::LDAP->new($uri . $srv) or
    die "Unable to connect to server: $@\n";

# !!! This is a temporary workaround !!!
my $binddn = "cn=Administrator,cn=users,dc=example,dc=com";
my $passwd = "password";

my $mesg = $c->bind($binddn, password => $passwd);
die 'Unable to bind: ' . $mesg->error . "\n" if $mesg->code;

# DN to be deleted
my $dn = param('DN');

$mesg = $c->delete($dn);
die 'Error in delete: '. $mesg->error() ."\n" if $mesg->code();

$c->unbind;

我可以用HTML表单调用这个cgi脚本,如:

<form action="/cgi-bin/del.cgi" method="post">
<br>Peter Parker
<input type="radio" name="DN"
    value="cn=peter parker,cn=users,dc=example,dc=com">
<br>Clark Kent
<input type="radio" name="DN"
    value="cn=clark kent,cn=users,dc=example,dc=com">
<br>
<input type="submit" value="Delete User">
</form>

此代码的问题是ldap操作使用管理凭据,而不是运行Web应用程序的用户的凭据 . 我正在使用此解决方法,因为我不能每次都要求用户提供他/她的凭据..而且我不知道如何让用户永久进行身份验证 .

我的Web应用程序通过ldap对用户进行身份验证,询问他的凭据并向目录服务发出绑定请求,如下所示:

...
# read user supplied credentials
my $user_id = param('user_id');
my $password = param('password');

# now find the DN of user_id in directory
my $ssl = 1;
my $srv = '192.168.56.110';
my $uri = $ssl ? 'ldaps://' : 'ldap://';

my $c = Net::LDAP->new($uri . $srv) or
    die "Unable to connect to server: $@";

# admin credentials are needed here to find the user DN
my $rootdn = "cn=Administrator,cn=users,dc=example,dc=com";
my $rootpw = "secret";

my $mesg = $c->bind($rootdn, password => $rootpw);
die "Unable to bind: ". $mesg->error if $mesg->code;

$mesg = $c->search(
    base    => 'dc=example,dc=com',
    scope   => 'sub',
    filter  => "(&(objectClass=user)(sAMAccountName=$user_id))",
    attrs   => ['sAMAccountName'],
);
die "Bad search: ". $mesg->error() if $mesg->code();

my ($entry) = $mesg->entries;
die "User not found: $user_id\n" unless $entry;

my $dn = $entry->dn;

# User DN found.. now check the credentials
$mesg = $c->bind($dn, password => $password);
die "Unable to bind: ". $mesg->error if $mesg->code;

$c->unbind();

# credentials validated!
print header, start_html('Welcome!'), h1('Hello, YOU!'), end_html;

之后,将cookie发送到启动Web会话的用户浏览器 . 我可以将用户凭据保存在数据库中,然后在我需要的任何时候将其传递给del.cgi(和其他类似的脚本)..但我不认为这是一个很好的安全实践 .

What can I do to keep a permanent ldap authenticated session as long as the web session is active?

1 回答

  • 0

    没有会话 . 当LDAP客户端连接到目录服务器时,连接未经身份验证 . 绑定请求,如果成功,则 Build 连接的授权状态 . 连接保持在该授权状态,直到下一个绑定请求,客户端断开连接或服务器断开连接 . 根据本地设置,可以使用保持活动或类似的方式无限期地保持连接打开 . 或者客户端可以定期发送另一个绑定请求 . 现代的,专业品质的目录服务器支持断开空闲客户端,或在经过一段时间后或在传输了一定数量的LDAP操作后断开客户端连接 . 请注意,网络管理员可能因其自身原因而不允许永久连接 .

    • LDAP客户端应在LDAP请求后检查响应控制 . 未能检查响应控件将导致客户端缺少来自服务器的重要信息 .

    • LDAP客户端必须知道服务器可以以扩展结果的形式发送未经请求的通知 . 未能处理未经请求的通知可能导致行为不正常的LDAP客户端 . 大多数通知都是断开连接通知,这意味着服务器因任何原因断开客户端连接 .

    有关更多信息,请参阅"LDAP: programming Practices" .

    出于好奇,为什么编码这样的东西? Apache Directory Studio是一个出色的LDAP客户端 .

相关问题