2008年11月19日水曜日

ActiveDirectoryのunicodePWDをLDAPSで変更する

とあるIdM製品ベンダの知り合いの方から「ADのunicodePWD属性ってLDAPSで変更できますよね?」と言われて、そういえば以前やったことあるな~というのを思い出しました。
 
3年ほど前に別blogに書いたネタがあるので転記しておきます。(いささか古いネタなので最近の環境での動作は微妙?)

■タイトル
 ActiveDirectoryのunicodePWD属性の変更をSolarisから行なう
■内容とサンプルスクリプト(当時のまま転記)

簡単に書くと、
1.サーバ証明書の発行(Windows証明書サービスで実施)
2.証明書をSolarisへインストール
3.Perl等のスクリプトでLDAPS接続して属性更新
となる。
注意点は、Windowsで証明書を発行する際に「Webサーバ」のテンプレートを使用して証明書を発行することくらい。

尚、必要物は以下のとおり。
【Windows側】
・ActiveDirectory
・証明書サービス
・IIS(証明書サービス用)
【Solaris側】
・OpenSSL
・Perl::LDAPSモジュール

【サンプルPerlスクリプト】
#!/usr/bin/perl -wuse strict;
use Net::LDAPS;

my($Ad, $mesg, $uid, $pass, $npass, $dn, $rtn);
$uid = "test";
$pass = "hoge";

print "Trying to set $uid to password $pass\n\n";
# Bind to the AD server
$Ad = Net::LDAPS->new("server.hoge.local",port => 636,capath => '/usr/local/ssl/certs/cacert.pem') or print "error connecting ad\n", exit 2;
$Ad->bind("CN=Administrator,CN=Users,DC=hoge,DC=local",password=>"password") or print "Unable to bind to AD server\n", exit 2;

# Do a AD lookup to get the dn for this user
# then change their password.
$mesg = $Ad->search(base => "ou=test,dc=hoge,dc=local", scope => "sub", filter => "(CN=test)");
if($mesg->count != 1) {
print "AD lookup failed for user $uid\n";
exit 3;
}
$dn = $mesg->entry(0)->dn;
map { $npass .= "$_\000" } split(//, "\"$pass\"");
print $dn;
$rtn = $Ad->modify($dn,replace=>["unicodePwd",$npass]);
if($rtn->{'resultCode'} != 0) {
print "User $uid, setting password failed\n";
exit 2;
}
print "Password for $uid changed in AD\n";
exit 0;

0 件のコメント: