aboutsummaryrefslogtreecommitdiff
path: root/files/usr/local/libexec/prosody-update-roster.xmpp_server
blob: 84c0c6e77985b8bc6e78866fc4ba35a076b4a787 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/local/bin/perl

use strict;
use warnings;

use Net::LDAP;
use Authen::SASL;

@ARGV == 2 or die "usage: $0 ROLE_NAME ROSTER_FILE\n";
my $role = $ARGV[0];
my $roster = $ARGV[1];

open my $fh, '<', '/usr/local/etc/openldap/ldap.conf' or quit($!);
my %config;
while (<$fh>) {
  chomp;
  next if /^#/;
  my @pair = split(' ', $_, 2);
  next unless (@pair == 2);
  $config{$pair[0]} = $pair[1];
}
close($fh);

my $mech         = $config{SASL_MECH}  // 'GSSAPI';
my $uri          = $config{URI}        // die("URI not specified\n");
my $users_basedn = $config{USERS_BASE} // die("USERS_BASE not specified\n");
my $roles_basedn = $config{ROLES_BASE} // die("ROLES_BASE not specified\n");

my $conn = Net::LDAP->new($uri, version => '3') or die "$@";
my $sasl = Authen::SASL->new($mech);
my $status = $conn->bind(sasl => $sasl);
$status->code and die $status->error;

my $search = $conn->search(
  scope  => 'sub',
  base   => $users_basedn,
  filter => "(&(memberOf=cn=$role,$roles_basedn)(mailAddress=*))",
  attrs  => ['mailAddress', 'cn']);

open $fh, '>', $roster or die "failed to open file for writing: $roster\n";
print $fh "[Internal]\n";
foreach my $entry ($search->entries) {
  my $jid = ($entry->get_value('mailAddress'))[0];
  my $cn = ($entry->get_value('cn'))[0] // $jid;
  print $fh "$jid=$cn\n";
}
close $fh;

system('prosodyctl reload');