2013-09-02 17:14:04 +00:00
|
|
|
#! /usr/bin/env perl
|
|
|
|
|
|
|
|
use v5.10.1;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Pod::Usage;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
push @INC, "../../";
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
use ACU::Log;
|
2013-09-02 17:14:04 +00:00
|
|
|
use ACU::LDAP;
|
|
|
|
use ACU::Process;
|
|
|
|
|
|
|
|
our $ou = "intra";
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
my %group_actions =
|
2013-09-02 17:14:04 +00:00
|
|
|
(
|
2013-09-02 19:09:51 +00:00
|
|
|
"new" => \&_new,
|
|
|
|
"add" => \&_add,
|
|
|
|
"delete" => \&_delete,
|
|
|
|
"flush" => \&_flush,
|
|
|
|
"remove" => \&_remove,
|
|
|
|
"update" => \&_update,
|
2013-09-02 17:14:04 +00:00
|
|
|
);
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
my %user_actions =
|
|
|
|
(
|
|
|
|
"add" => \&_add,
|
|
|
|
"flush" => \&_flush,
|
|
|
|
"remove" => \&_remove,
|
|
|
|
"update" => \&_update,
|
|
|
|
);
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $_get_type;
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub _new($$$)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
2013-09-02 18:43:18 +00:00
|
|
|
my $ldap = shift;
|
2013-09-02 17:14:04 +00:00
|
|
|
my $dn = shift;
|
|
|
|
my $args = shift;
|
|
|
|
|
|
|
|
# Add group
|
2013-09-02 19:09:51 +00:00
|
|
|
if (LDAP::add_group($ldap, $args->{param}{cn}, $args->{param}{year}) eq $dn)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
|
|
|
if ($args->{param}{type}) {
|
2013-09-02 19:09:51 +00:00
|
|
|
group_add $dn, $args;
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub _add($$$)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
2013-09-02 18:43:18 +00:00
|
|
|
my $ldap = shift;
|
2013-09-02 17:14:04 +00:00
|
|
|
my $dn = shift;
|
|
|
|
my $args = shift;
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $cnt_type = $_get_type->($args->{param});
|
2013-09-02 17:14:04 +00:00
|
|
|
|
2013-09-04 00:56:29 +00:00
|
|
|
ACU::Log::do_dump($args);
|
|
|
|
|
2013-09-02 17:14:04 +00:00
|
|
|
# Add content if any
|
|
|
|
for (my $i = $args->{unamed}; $i > 0; $i--) {
|
2013-09-02 19:09:51 +00:00
|
|
|
LDAP::add_attribute($ldap, $dn, $cnt_type, $args->{param}{$i});
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub _delete($$$)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
2013-09-02 18:43:18 +00:00
|
|
|
return LDAP::delete_entry($_[0], $_[1]);
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub _flush($$)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
2013-09-02 18:43:18 +00:00
|
|
|
my $ldap = shift;
|
2013-09-02 17:14:04 +00:00
|
|
|
my $dn = shift;
|
|
|
|
my $args = shift;
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $cnt_type = $_get_type->($args->{param});
|
2013-09-02 17:14:04 +00:00
|
|
|
|
2013-09-02 18:43:18 +00:00
|
|
|
return LDAP::flush_attribute($ldap, $dn, $cnt_type);
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub _remove($$)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
2013-09-02 18:43:18 +00:00
|
|
|
my $ldap = shift;
|
2013-09-02 17:14:04 +00:00
|
|
|
my $dn = shift;
|
|
|
|
my $args = shift;
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $cnt_type = $_get_type->($args->{param});
|
2013-09-02 17:14:04 +00:00
|
|
|
|
|
|
|
my @data;
|
|
|
|
for (my $i = $args->{unamed}; $i > 0; $i--) {
|
2013-09-03 06:07:29 +00:00
|
|
|
push @data, $args->{param}{$i};
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 06:07:29 +00:00
|
|
|
return LDAP::delete_attribute($ldap, $dn, $cnt_type, @data);
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub _update($$)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
2013-09-02 18:43:18 +00:00
|
|
|
my $ldap = shift;
|
2013-09-02 17:14:04 +00:00
|
|
|
my $dn = shift;
|
|
|
|
my $args = shift;
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $cnt_type = $_get_type->($args->{param});
|
|
|
|
|
|
|
|
return user_update($ldap, $dn, $args) if ($cnt_type eq "userInfos");
|
2013-09-02 17:14:04 +00:00
|
|
|
|
|
|
|
my @data;
|
|
|
|
for (my $i = $args->{unamed}; $i > 0; $i--) {
|
2013-09-03 06:07:29 +00:00
|
|
|
push @data, $args->{param}{$i};
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
return LDAP::update_attribute($ldap, $dn, $cnt_type, @data);
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
sub user_update($$)
|
|
|
|
{
|
|
|
|
my $ldap = shift;
|
|
|
|
my $dn = shift;
|
|
|
|
my $args = shift;
|
2013-09-02 17:14:04 +00:00
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
LDAP::update_attribute($ldap, $dn, "cn", $args->{param}{cn}) if ($args->{param}{cn});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "cn", $args->{param}{firstname}." ".$args->{param}{lastname}) if ($args->{param}{firstname} && $args->{param}{lastname});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "l", $args->{param}{l}) if ($args->{param}{l});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "mail", $args->{param}{mail}) if ($args->{param}{mail});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "postalAddress", $args->{param}{postalAddress}) if ($args->{param}{postalAddress});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "postalCode", $args->{param}{postalCode}) if ($args->{param}{postalCode});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "sn", $args->{param}{sn}) if ($args->{param}{sn});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "telephoneNumber", $args->{param}{telephoneNumber}) if ($args->{param}{telephoneNumber});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "sshPublicKey", $args->{param}{sshPublicKey}) if ($args->{param}{sshPublicKey});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "strongAuthKey", $args->{param}{strongAuthKey}) if ($args->{param}{strongAuthKey});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "c", $args->{param}{c}) if ($args->{param}{c});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "title", $args->{param}{title}) if ($args->{param}{title});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "intraRight", $args->{param}{intraRight}) if ($args->{param}{intraRight});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "intraTheme", $args->{param}{intraTheme}) if ($args->{param}{intraTheme});
|
|
|
|
LDAP::update_attribute($ldap, $dn, "birthdate", $args->{param}{birthdate}) if ($args->{param}{birthdate});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub group_get_type($)
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
|
|
|
my $param = shift;
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $type = $param->{type} // "members";
|
|
|
|
|
2013-09-02 17:14:04 +00:00
|
|
|
# Extract data type
|
2013-09-03 05:20:58 +00:00
|
|
|
if ($type eq "members") {
|
2013-09-02 17:14:04 +00:00
|
|
|
return "memberUid";
|
|
|
|
}
|
2013-09-03 05:20:58 +00:00
|
|
|
elsif ($type eq "rights") {
|
|
|
|
return "intraRights" ;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die ("Unknown type to add: ".$type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub user_get_type($)
|
|
|
|
{
|
|
|
|
my $param = shift;
|
|
|
|
|
|
|
|
my $type = $param->{type} // "userInfos";
|
|
|
|
|
|
|
|
# Extract data type
|
|
|
|
if ($type eq "rights") {
|
2013-09-02 17:14:04 +00:00
|
|
|
return "intraRights" ;
|
|
|
|
}
|
2013-09-03 05:20:58 +00:00
|
|
|
elsif ($type eq "sshkeys") {
|
|
|
|
return "sshPublicKey" ;
|
|
|
|
}
|
|
|
|
elsif ($type eq "userInfos") {
|
|
|
|
return "userInfos" ;
|
|
|
|
}
|
2013-09-02 17:14:04 +00:00
|
|
|
else {
|
2013-09-03 05:20:58 +00:00
|
|
|
die ("Unknown type to add: ".$type);
|
2013-09-02 17:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
sub process_group
|
2013-09-02 17:14:04 +00:00
|
|
|
{
|
|
|
|
my ($given_args, $args) = @_;
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
my $year = $args->{param}{year} // LDAP::get_year;
|
|
|
|
my $dn = "cn=".$args->{param}{cn}."ou=$year,ou=$ou,ou=groups,dc=acu,dc=epita,dc=fr";
|
2013-09-03 05:20:58 +00:00
|
|
|
my $action = $args->{param}{action} // "update";
|
2013-09-02 17:14:04 +00:00
|
|
|
|
|
|
|
# Read action
|
2013-09-02 19:09:51 +00:00
|
|
|
if (! exists $group_actions{$action}) {
|
2013-09-02 17:14:04 +00:00
|
|
|
return "Unknown command for update_group: ". $action;
|
|
|
|
}
|
|
|
|
|
2013-09-02 18:43:18 +00:00
|
|
|
my $ldap = LDAP::ldap_connect();
|
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
$group_actions{$action}($ldap, $dn, $args);
|
|
|
|
|
|
|
|
$ldap->unbind or warn "couldn't disconnect correctly";
|
2013-09-03 05:20:58 +00:00
|
|
|
|
|
|
|
return "Ok";
|
2013-09-02 19:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_user
|
|
|
|
{
|
|
|
|
my ($given_args, $args) = @_;
|
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
my $action = $args->{param}{action} // "update";
|
2013-09-02 19:09:51 +00:00
|
|
|
|
|
|
|
# Read action
|
|
|
|
if (! exists $user_actions{$action}) {
|
|
|
|
return "Unknown command for update_user: ". $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $ldap = LDAP::ldap_connect();
|
|
|
|
|
|
|
|
my $dn = LDAP::search_dn($ldap, "ou=users", "uid=".$args->{param}{uid});
|
2013-09-02 18:43:18 +00:00
|
|
|
|
2013-09-02 19:09:51 +00:00
|
|
|
$user_actions{$action}($ldap, $dn, $args);
|
|
|
|
|
|
|
|
$ldap->unbind or warn "couldn't disconnect correctly";
|
2013-09-02 17:14:04 +00:00
|
|
|
|
2013-09-03 05:20:58 +00:00
|
|
|
return "Ok";
|
|
|
|
}
|
2013-09-02 19:09:51 +00:00
|
|
|
|
|
|
|
if ($0 =~ /^update_group/) {
|
2013-09-03 05:20:58 +00:00
|
|
|
$_get_type = \&group_get_type;
|
2013-09-02 19:09:51 +00:00
|
|
|
Process::register("update_group", \&process_group);
|
|
|
|
}
|
|
|
|
elsif ($0 =~ /^update_user/) {
|
2013-09-03 05:20:58 +00:00
|
|
|
$_get_type = \&user_get_type;
|
2013-09-02 19:09:51 +00:00
|
|
|
Process::register("update_user", \&process_user);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die ("Bad filename.");
|
|
|
|
}
|