Skip to content

[WIP] Add LDAP sync to Revbank #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions plugins/ldap_sync
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!perl
use strict;
use warnings;
use Net::LDAP;
use POSIX qw(strftime);
use File::HomeDir;
use File::Spec;
use Getopt::Long;

# --- CONFIGURATION ---
my $ldap_host = 'ldap://localhost';
my $ldap_bind_dn = 'cn=admin,dc=example,dc=com';
my $ldap_bind_pw = 'secret';
my $ldap_base = 'ou=People,dc=example,dc=com';

my $revbank_file = File::Spec->catfile(File::HomeDir->my_home, '.revbank', 'accounts');
my $log_file = '/var/log/revbank_ldap_sync.log';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RevBank normally doesn't write outside its own data directory.


# --- OPTIONS ---
my $dry_run = 0;
GetOptions('dry-run' => \$dry_run) or die "Usage: $0 [--dry-run]\n";

# --- Logging sub ---
sub logmsg {
my ($msg) = @_;
my $ts = strftime("%Y-%m-%d %H:%M:%S", localtime);
my $line = "[$ts] $msg\n";
print $line;
open my $logfh, '>>', $log_file or die "Can't open log file $log_file: $!";
print $logfh $line;
close $logfh;
}

# --- Main ---
logmsg("Starting sync" . ($dry_run ? " [DRY RUN]" : ""));

my @ldap_users = get_ldap_users();
my @revbank_users = get_revbank_users($revbank_file);

# Remove duplicates
my %seen;
@ldap_users = grep { !$seen{$_}++ } @ldap_users;
%seen = ();
@revbank_users = grep { !$seen{$_}++ } @revbank_users;

my %ldap_hash = map { $_ => 1 } @ldap_users;
my %revbank_hash = map { $_ => 1 } @revbank_users;

my @only_in_ldap = grep { !$revbank_hash{$_} } @ldap_users;
my @only_in_revbank = grep { !$ldap_hash{$_} } @revbank_users;

foreach my $user (@only_in_ldap) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plugins typically run their code in hooks, or the command sub. Maybe this is intended as a script to run from a regular shell? If so, the file should probably go in the contrib/ directory instead of plugins/.

if ($dry_run) {
logmsg("Would add user '$user' to Revbank");
} else {
add_to_revbank($user, $revbank_file);
}
}

foreach my $user (@only_in_revbank) {
if ($dry_run) {
logmsg("Would add user '$user' to LDAP");
} else {
add_to_ldap($user);
}
}

logmsg("Sync finished");

# --- Functions ---

sub get_ldap_users {
my $ldap = Net::LDAP->new($ldap_host) or die "LDAP connect failed: $@";
my $mesg = $ldap->bind($ldap_bind_dn, password => $ldap_bind_pw);
die "LDAP bind failed: ", $mesg->error if $mesg->code;

$mesg = $ldap->search(
base => $ldap_base,
scope => 'sub',
filter => '(objectClass=posixAccount)',
attrs => ['uid'],
);
die "LDAP search failed: ", $mesg->error if $mesg->code;

my @uids;
for my $entry ($mesg->entries) {
my $uid = $entry->get_value('uid');
push @uids, $uid if $uid;
}

$ldap->unbind;
return @uids;
}

sub get_revbank_users {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use RevBank::Accounts::names() for this.

my ($file) = @_;
open my $fh, '<', $file or die "Could not open revbank file: $file: $!";
my @users;
while (<$fh>) {
next if /^\s*-/; # skip system transactions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System accounts currently begin with +, -, or *. You could use RevBank::Accounts::is_special() instead of a regex, so your logic always aligns with what revbank uses.

if (/^(\w+)\s+/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RevBank has valid user names that start with a non-\w character.

push @users, $1;
}
}
close $fh;
return @users;
}

sub add_to_revbank {
my ($user, $file) = @_;
my $ts = strftime("%Y-%m-%d_%H:%M:%S", localtime);
my $line = sprintf "%-20s +0.00 %s +@%s\n", $user, $ts, $ts;

open my $fh, '>>', $file or die "Could not write to $file: $!";
print $fh $line;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use RevBank::Accounts::create() for this.

If you're rolling your own implementation, consider at least supporting the global lock.

close $fh;

logmsg("Added $user to Revbank");
}

sub add_to_ldap {
my ($user) = @_;
my $ldap = Net::LDAP->new($ldap_host) or die "LDAP connect failed: $@";
my $mesg = $ldap->bind($ldap_bind_dn, password => $ldap_bind_pw);
die "LDAP bind failed: ", $mesg->error if $mesg->code;

my $uidNumber = 20000 + int(rand(10000));
my $gidNumber = 100;
my $dn = "uid=$user,$ldap_base";

$mesg = $ldap->add($dn,
attrs => [
objectClass => ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount'],
cn => $user,
sn => $user,
uid => $user,
uidNumber => $uidNumber,
gidNumber => $gidNumber,
homeDirectory => "/home/$user",
loginShell => '/bin/bash',
]
);

if ($mesg->code) {
logmsg("Failed to add $user to LDAP: " . $mesg->error);
} else {
logmsg("Added $user to LDAP");
}

$ldap->unbind;
}