Import old synchornization scripts by aangel

This commit is contained in:
Timothée Floure 2018-06-28 09:30:47 +02:00
commit 8fccf87a94
3 changed files with 211 additions and 0 deletions

View file

@ -0,0 +1,2 @@
#!/bin/sh
exec "$(dirname "$0")/mlmmj-unipoly-ldap-listen.pl" 2>&1 | logger --priority daemon.info -t unipoly-mlmmj-ldap-sync

View file

@ -0,0 +1,110 @@
#!/usr/bin/perl
# Copyright (c) 2014 Axel Angel <axel-oss@vneko.ch>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
# require: https://github.com/axel-angel/ldap-sync-listener
# require: libnet-ldap-perl libtime-out-perl
use strict;
use warnings;
use Time::Out qw(timeout);
use Net::LDAP::SyncDiffListener;
use Net::LDAP::Util qw{ldap_explode_dn};
my $host = 'ldaps://gnusrv2.epfl.ch';
my $base = 'ou=Lists,dc=unipoly,dc=epfl,dc=ch';
my $user = 'cn=unipoly-mlmmj,ou=Services,dc=unipoly,dc=epfl,dc=ch';
my $pass = 'secretsecretsecretsecret';
my @lists = qw{amis info news membres};
my $mlmmj_folder = '/var/spool/mlmmj/';
my $reco_interval = 86400; # 24 hours
sub mlmmj_sub($$) {
my ($list, $addr) = @_;
print "mlmmj_sub $addr to $list\n";
my $folder = $mlmmj_folder . $list;
system('/usr/bin/mlmmj-sub', '-L', $folder, '-a', $addr);
warn "mlmml exited with code ", $? >> 8 ,": $!" if $?;
}
sub mlmmj_unsub($$) {
my ($list, $addr) = @_;
print "mlmmj_unsub $addr from $list\n";
my $folder = $mlmmj_folder . $list;
system('/usr/bin/mlmmj-unsub', '-L', $folder, '-a', $addr);
}
my %search = (
base => $base,
scope => 'sub',
filter => "(objectClass=groupOfUniqueNames)",
attrs => ['*'],
);
sub handle_change($$$$) {
my ($isadd, $entry, $attr, $value) = @_;
my $cn = $entry->get_value('cn');
print "Handle change cn={$cn} attr={$attr} value={$value}\n";
unless ($cn ~~ @lists) {
warn "We don't manage: $cn";
return;
}
if ($attr ne "uniqueMember") {
warn "We don't care about attr: $attr";
return;
}
my $list = $cn .'@unipoly.ch';
my $addr = ldap_explode_dn($value)->[0]{MAIL};
my $action = $isadd ? \&mlmmj_sub : \&mlmmj_unsub;
$action->($list, $addr);
}
my %callbacks = (
add_entry => sub {
my ($entry) = @_;
print "add_entry: ", $entry->dn(), "\n";
},
del_entry => sub {
my ($dn) = @_;
print "del_entry: ", $dn, "\n";
},
add_attr_value => sub {
my ($entry, $attr, $value) = @_;
handle_change(1, $entry, $attr, $value);
},
del_attr_value => sub {
my ($entry, $attr, $value) = @_;
handle_change(0, $entry, $attr, $value);
},
);
# restart after a delay, make sure connection is still alive
my $state = $ENV{HOME} .'/mlmmj-unipoly-ldap.state.yaml';
while (1) {
my $ldap = Net::LDAP::SyncDiffListener->new($host, keepalive => 1)
or die "$@";
$ldap->bind($user, password => $pass);
my $trapper = sub {
$ldap->unbind;
$ldap->disconnect;
die "Stopped as requested";
};
$SIG{INT} = $trapper;
$SIG{TERM} = $trapper;
$SIG{HUP} = $trapper;
timeout $reco_interval => sub {
$ldap->listen($state, \%search, \%callbacks);
};
$ldap->unbind;
$ldap->disconnect;
print "Timed out, restarting\n";
}

99
legacy/unipoly-mlmmj-ldap-sync Executable file
View file

@ -0,0 +1,99 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: unipoly-mlmmj-ldap-sync
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Should-Start: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 1
# Short-Description: daemon to push ldap changes to mlmmj for Unipoly
### END INIT INFO
# Author: Axel Angel for GNU Generation and Unipoly
APPDIR=/var/spool/exim4
PATH=/var/spool/exim4/bin:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Unipoly mlmmj ldap sync"
NAME=unipoly-mlsync
EXE=$APPDIR/bin/mlmmj-unipoly-ldap-listen
EXESRV=$APPDIR/bin/mlmmj-unipoly-ldap-listen.pl
DAEMON=$APPDIR/bin/mlmmj-unipoly-ldap-listen
DAEMON_UID=Debian-exim
DAEMON_GID=Debian-exim
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x $DAEMON ] || exit 0
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
. /lib/lsb/init-functions
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
#
# Function that starts the daemon/service
#
do_start()
{
start-stop-daemon --start --quiet --exec $EXE \
--umask 0027 --chuid $DAEMON_UID:$DAEMON_GID \
--chdir "$APPDIR" --background -- \
|| return 1
}
#
# Function that stops the daemon/service
#
do_stop()
{
pkill -u "$DAEMON_UID" -f "$APPDIR/bin/mlmmj-unipoly-ldap-listen.pl"
}
case "$1" in
start)
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
: