2010-02-26 21:26:53 +00:00
|
|
|
# IBM(c) 2010 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT_plugin::activedirectory;
|
|
|
|
BEGIN
|
|
|
|
{
|
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
|
|
|
|
}
|
|
|
|
my $callback;
|
|
|
|
use lib "$::XCATROOT/lib/perl";
|
2010-02-26 22:15:14 +00:00
|
|
|
use Getopt::Long;
|
2010-03-02 21:54:37 +00:00
|
|
|
use xCAT::ADUtils;
|
|
|
|
use Net::DNS;
|
2010-08-06 15:29:07 +00:00
|
|
|
use xCAT::SvrUtils;
|
2010-03-05 13:49:22 +00:00
|
|
|
use strict;
|
2010-03-02 21:54:37 +00:00
|
|
|
|
2010-02-26 21:26:53 +00:00
|
|
|
sub handled_commands {
|
|
|
|
return {
|
2010-03-08 17:25:31 +00:00
|
|
|
clusteruseradd => 'site:directoryprovider',
|
|
|
|
clusteruserdel => 'site:directoryprovider',
|
|
|
|
clusteruserlist => 'site:directoryprovider',
|
|
|
|
hostaccountlist => 'site:directoryprovider',
|
|
|
|
hostaccountadd => 'site:directoryprovider',
|
|
|
|
hostaccountdel => 'site:directoryprovider',
|
2010-02-26 21:26:53 +00:00
|
|
|
};
|
|
|
|
}
|
2010-03-02 21:54:37 +00:00
|
|
|
|
2010-02-26 21:26:53 +00:00
|
|
|
sub process_request {
|
2010-05-19 12:54:29 +00:00
|
|
|
$ENV{LDAPRC}='ad.ldaprc'; #after examining openldap source, this is the best way to get their libraries to source /etc/xcat/ad.ldaprc
|
|
|
|
$ENV{HOME}='/etc/xcat';
|
2010-02-26 21:26:53 +00:00
|
|
|
my $request = shift;
|
2010-02-26 22:15:14 +00:00
|
|
|
my $command = $request->{command}->[0];
|
|
|
|
$callback = shift;
|
2010-02-26 21:26:53 +00:00
|
|
|
my $doreq = shift;
|
|
|
|
use Data::Dumper;
|
2010-03-04 14:58:45 +00:00
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
my $domain;
|
|
|
|
$domain = $sitetab->getAttribs({key=>'domain'},['value']);
|
|
|
|
if ($domain and $domain->{value}) {
|
|
|
|
$domain = $domain->{value};
|
|
|
|
} else {
|
|
|
|
$domain = undef;
|
|
|
|
}
|
|
|
|
#TODO: if multi-domain support implemented, use the domains table to reference between realm and domain
|
|
|
|
my $server = $sitetab->getAttribs({key=>'directoryserver'},['value']);
|
|
|
|
my $realm = $sitetab->getAttribs({key=>'realm'},['value']);
|
|
|
|
if ($realm and $realm->{value}) {
|
|
|
|
$realm = $realm->{value};
|
|
|
|
} else {
|
|
|
|
$realm = uc($domain);
|
|
|
|
$realm =~ s/\.$//; #remove trailing dot if provided
|
|
|
|
}
|
|
|
|
my $passtab = xCAT::Table->new('passwd');
|
|
|
|
my $adpent = $passtab->getAttribs({key=>'activedirectory'},[qw/username password/]);
|
|
|
|
unless ($adpent and $adpent->{username} and $adpent->{password}) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"activedirectory entry missing from passwd table"], $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ($server and $server->{value}) {
|
|
|
|
$server = $server->{value};
|
|
|
|
} else {
|
|
|
|
my $res = Net::DNS::Resolver->new;
|
|
|
|
my $query = $res->query("_ldap._tcp.$domain","SRV");
|
|
|
|
if ($query) {
|
|
|
|
foreach my $srec ($query->answer) {
|
|
|
|
$server = $srec->{target};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unless ($server) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine a directory server to communicate with, try site.directoryserver"], $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-03-08 17:25:31 +00:00
|
|
|
if ($command =~ /userlist/) { #user management command, listing
|
2010-03-04 14:58:45 +00:00
|
|
|
my $passwdfmt;
|
2010-03-05 13:49:22 +00:00
|
|
|
if ($request->{arg}) {
|
|
|
|
@ARGV=@{$request->{arg}};
|
|
|
|
Getopt::Long::Configure("bundling");
|
|
|
|
Getopt::Long::Configure("no_pass_through");
|
|
|
|
if (!GetOptions(
|
|
|
|
'p' => \$passwdfmt
|
|
|
|
)) {
|
|
|
|
die "TODO: usage message";
|
|
|
|
}
|
2010-03-04 14:58:45 +00:00
|
|
|
}
|
|
|
|
unless ($domain and $realm) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine domain from arguments or site table"], $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
my $err = xCAT::ADUtils::krb_login(username=>$adpent->{username},password=>$adpent->{password},realm=>$realm);
|
|
|
|
if ($err) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Error authenticating to Active Directory"], $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
my $accounts = xCAT::ADUtils::list_user_accounts(
|
|
|
|
dnsdomain => $domain,
|
|
|
|
directoryserver=> $server,
|
|
|
|
);
|
|
|
|
if ($passwdfmt) {
|
|
|
|
my $account;
|
|
|
|
foreach $account (keys %$accounts) {
|
|
|
|
my $textout = ":".$account.":x:"; #first colon is because sendmsg would mistake it for a description
|
|
|
|
foreach (qw/uid gid fullname homedir shell/) {
|
|
|
|
$textout .= $accounts->{$account}->{$_}.":";
|
|
|
|
}
|
|
|
|
$textout =~ s/:$//;
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg($textout, $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my $account;
|
|
|
|
foreach $account (keys %$accounts) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg($account, $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-08 17:25:31 +00:00
|
|
|
} elsif ($command =~ /hostaccountlist/) {
|
|
|
|
unless ($domain and $realm) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine domain from arguments or site table"], $callback);
|
2010-03-08 17:25:31 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
my $err = xCAT::ADUtils::krb_login(username=>$adpent->{username},password=>$adpent->{password},realm=>$realm);
|
|
|
|
if ($err) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Error authenticating to Active Directory"], $callback);
|
2010-03-08 17:25:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
my $accounts = xCAT::ADUtils::list_host_accounts(
|
|
|
|
dnsdomain => $domain,
|
|
|
|
directoryserver=> $server,
|
|
|
|
);
|
|
|
|
my $account;
|
|
|
|
foreach $account (keys %$accounts) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg($account, $callback);
|
2010-03-08 17:25:31 +00:00
|
|
|
}
|
|
|
|
} elsif ($command =~ /hostaccountdel/) {
|
|
|
|
my $accountname;
|
|
|
|
my %loggedrealms = ();
|
|
|
|
foreach $accountname (@{$request->{node}}) {
|
|
|
|
if ($request->{arg} and scalar @{$request->{arg}}) {
|
|
|
|
die "TODO: usage";
|
|
|
|
}
|
|
|
|
if ($accountname =~ /@/) {
|
|
|
|
($accountname,$domain) = split /@/,$accountname;
|
|
|
|
$domain = lc($domain);
|
|
|
|
}
|
|
|
|
unless ($domain) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine domain from arguments or site table"], $callback);
|
2010-03-08 17:25:31 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
#my $domainstab = xCAT::Table->new('domains');
|
|
|
|
#$realm = $domainstab->getAttribs({domain=>$domain},
|
|
|
|
unless ($realm) {
|
|
|
|
$realm = uc($domain);
|
|
|
|
$realm =~ s/\.$//; #remove trailing dot if provided
|
|
|
|
}
|
|
|
|
$ENV{KRB5CCNAME}="/tmp/xcat/krbcache.$realm.$$";
|
|
|
|
unless ($loggedrealms{$realm}) {
|
|
|
|
my $err = xCAT::ADUtils::krb_login(username=>$adpent->{username},password=>$adpent->{password},realm=>$realm);
|
|
|
|
if ($err) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Error authenticating to Active Directory"], $callback,$accountname);
|
2010-03-08 17:25:31 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
$loggedrealms{$realm}=1;
|
|
|
|
}
|
|
|
|
my %args = (
|
|
|
|
account => $accountname,
|
|
|
|
dnsdomain => $domain,
|
|
|
|
directoryserver=> $server,
|
|
|
|
);
|
|
|
|
my $ret = xCAT::ADUtils::del_host_account(%args);
|
|
|
|
}
|
|
|
|
foreach my $realm (keys %loggedrealms) {
|
|
|
|
unlink "/tmp/xcat/krbcache.$realm.$$";
|
|
|
|
}
|
|
|
|
} elsif ($command =~ /userdel/) {
|
|
|
|
my $username = shift @{$request->{arg}};
|
2010-03-05 14:25:56 +00:00
|
|
|
if (scalar @{$request->{arg}}) {
|
|
|
|
die "TODO: usage";
|
|
|
|
}
|
|
|
|
if ($username =~ /@/) {
|
|
|
|
($username,$domain) = split /@/,$username;
|
|
|
|
$domain = lc($domain);
|
|
|
|
}
|
|
|
|
unless ($domain) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine domain from arguments or site table"], $callback);
|
2010-03-05 14:25:56 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
#my $domainstab = xCAT::Table->new('domains');
|
|
|
|
#$realm = $domainstab->getAttribs({domain=>$domain},
|
|
|
|
unless ($realm) {
|
|
|
|
$realm = uc($domain);
|
|
|
|
$realm =~ s/\.$//; #remove trailing dot if provided
|
|
|
|
}
|
2010-03-08 17:25:31 +00:00
|
|
|
$ENV{KRB5CCNAME}="/tmp/xcat/krbcache.$realm.$$";
|
2010-03-05 14:25:56 +00:00
|
|
|
|
|
|
|
my $err = xCAT::ADUtils::krb_login(username=>$adpent->{username},password=>$adpent->{password},realm=>$realm);
|
|
|
|
if ($err) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Error authenticating to Active Directory"], $callback);
|
2010-03-05 14:25:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-03-08 17:25:31 +00:00
|
|
|
my %args = (
|
|
|
|
account => $username,
|
|
|
|
dnsdomain => $domain,
|
|
|
|
directoryserver=> $server,
|
|
|
|
);
|
|
|
|
if ($command =~ /userdel/) {
|
|
|
|
my $ret = xCAT::ADUtils::del_user_account(%args);
|
|
|
|
} elsif ($command =~ /hostaccountdel/) {
|
|
|
|
my $ret = xCAT::ADUtils::del_host_account(%args);
|
|
|
|
}
|
|
|
|
} elsif ($command =~ /useradd$/) { #user management command, adding
|
2010-02-26 22:15:14 +00:00
|
|
|
my $homedir;
|
|
|
|
my $fullname;
|
|
|
|
my $gid;
|
|
|
|
my $uid;
|
2010-03-02 21:54:37 +00:00
|
|
|
my $ou;
|
2010-02-26 22:15:14 +00:00
|
|
|
@ARGV=@{$request->{arg}};
|
|
|
|
Getopt::Long::Configure("bundling");
|
2010-03-02 21:54:37 +00:00
|
|
|
Getopt::Long::Configure("no_pass_through");
|
2010-02-26 22:15:14 +00:00
|
|
|
|
|
|
|
if (!GetOptions(
|
|
|
|
'd=s' => \$homedir,
|
|
|
|
'c=s' => \$fullname,
|
|
|
|
'g=s' => \$gid,
|
2010-03-02 21:54:37 +00:00
|
|
|
'o=s' => \$ou,
|
2010-02-26 22:15:14 +00:00
|
|
|
'u=s' => \$uid)) {
|
2010-03-02 21:54:37 +00:00
|
|
|
die "TODO: usage message";
|
2010-02-26 22:15:14 +00:00
|
|
|
}
|
|
|
|
my $username = shift @ARGV;
|
2010-03-02 21:54:37 +00:00
|
|
|
if ($username =~ /@/) {
|
|
|
|
($username,$domain) = split /@/,$username;
|
|
|
|
$domain = lc($domain);
|
2010-03-04 14:58:45 +00:00
|
|
|
}
|
|
|
|
unless ($domain) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine domain from arguments or site table"], $callback);
|
2010-03-04 14:58:45 +00:00
|
|
|
return undef;
|
2010-03-02 21:54:37 +00:00
|
|
|
}
|
2010-02-26 22:15:14 +00:00
|
|
|
|
2010-03-02 21:54:37 +00:00
|
|
|
#my $domainstab = xCAT::Table->new('domains');
|
|
|
|
#$realm = $domainstab->getAttribs({domain=>$domain},
|
2010-03-04 14:58:45 +00:00
|
|
|
unless ($realm) {
|
|
|
|
$realm = uc($domain);
|
|
|
|
$realm =~ s/\.$//; #remove trailing dot if provided
|
2010-03-02 21:54:37 +00:00
|
|
|
}
|
2010-02-26 22:15:14 +00:00
|
|
|
|
2010-03-02 21:54:37 +00:00
|
|
|
my $err = xCAT::ADUtils::krb_login(username=>$adpent->{username},password=>$adpent->{password},realm=>$realm);
|
|
|
|
if ($err) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Error authenticating to Active Directory"], $callback);
|
2010-03-02 21:54:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
my %args = (
|
|
|
|
username => $username,
|
|
|
|
dnsdomain => $domain,
|
|
|
|
directoryserver=> $server,
|
2010-03-04 14:58:45 +00:00
|
|
|
);
|
2010-03-02 21:54:37 +00:00
|
|
|
if ($fullname) { $args{fullname} = $fullname };
|
|
|
|
if ($ou) { $args{ou} = $ou };
|
|
|
|
if ($request->{environment} and
|
|
|
|
$request->{environment}->[0]->{XCAT_USERPASS}) {
|
|
|
|
$args{password} = $request->{environment}->[0]->{XCAT_USERPASS}->[0];
|
|
|
|
}
|
|
|
|
#TODO: args password
|
|
|
|
if (defined $gid) { $args{gid} = $gid };
|
|
|
|
if (defined $uid) { $args{uid} = $uid };
|
|
|
|
#TODO: smbHome for windows
|
|
|
|
if (defined $homedir) { $args{homedir} = $homedir };
|
|
|
|
my $ret = xCAT::ADUtils::add_user_account(%args);
|
|
|
|
if (ref $ret and $ret->{error}) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,$ret->{error}], $callback);
|
2010-03-02 21:54:37 +00:00
|
|
|
}
|
2010-03-08 17:25:31 +00:00
|
|
|
} elsif ($command =~ /hostaccountadd$/) { #user management command, adding
|
|
|
|
my $ou;
|
|
|
|
if ($request->{arg}) {
|
|
|
|
@ARGV=@{$request->{arg}};
|
|
|
|
Getopt::Long::Configure("bundling");
|
|
|
|
Getopt::Long::Configure("no_pass_through");
|
|
|
|
|
|
|
|
if (!GetOptions('o=s' => \$ou)) {
|
|
|
|
die "TODO: usage message";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#my $domainents = $domaintab->getNodesAttribs($request->{node},['ou','domain']); #TODO: have this in schema
|
|
|
|
my $nodename;
|
|
|
|
my %loggedrealms=();
|
|
|
|
foreach $nodename (@{$request->{node}}) {
|
|
|
|
if ($nodename =~ /\./) {
|
|
|
|
($nodename,$domain) = split /\./,$nodename,2;
|
|
|
|
$domain = lc($domain);
|
|
|
|
}
|
|
|
|
unless ($domain) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Unable to determine domain from arguments or site table"], $callback);
|
2010-03-08 17:25:31 +00:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
#my $domainstab = xCAT::Table->new('domains');
|
|
|
|
#$realm = $domainstab->getAttribs({domain=>$domain},
|
|
|
|
unless ($realm) {
|
|
|
|
$realm = uc($domain);
|
|
|
|
$realm =~ s/\.$//; #remove trailing dot if provided
|
|
|
|
}
|
|
|
|
unless ($loggedrealms{$realm}) {
|
|
|
|
my $err = xCAT::ADUtils::krb_login(username=>$adpent->{username},password=>$adpent->{password},realm=>$realm);
|
|
|
|
if ($err) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,"Error authenticating to Active Directory"], $callback,$nodename);
|
2010-03-08 17:25:31 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
$loggedrealms{$realm}=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my %args = (
|
|
|
|
node => $nodename,
|
|
|
|
dnsdomain => $domain,
|
|
|
|
directoryserver=> $server,
|
|
|
|
);
|
|
|
|
if ($ou) { $args{ou} = $ou };
|
|
|
|
if ($request->{environment} and
|
|
|
|
$request->{environment}->[0]->{XCAT_HOSTPASS}) {
|
|
|
|
$args{password} = $request->{environment}->[0]->{XCAT_HOSTPASS}->[0];
|
|
|
|
}
|
|
|
|
my $ret = xCAT::ADUtils::add_host_account(%args);
|
|
|
|
if (ref $ret and $ret->{error}) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg([1,$ret->{error}], $callback);
|
2010-03-08 17:25:31 +00:00
|
|
|
} elsif (ref $ret) {
|
2010-08-06 15:29:07 +00:00
|
|
|
xCAT::SvrUtils::sendmsg($ret->{password}, $callback,$nodename);
|
2010-03-08 17:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-02 21:54:37 +00:00
|
|
|
}
|
2010-02-26 21:26:53 +00:00
|
|
|
}
|
|
|
|
|
2010-02-26 22:15:14 +00:00
|
|
|
1;
|