File: ldap-switch-sendas.pl

package info (click to toggle)
kopanocore 8.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,400 kB
  • sloc: cpp: 175,422; python: 24,623; perl: 7,319; php: 6,056; sh: 2,172; makefile: 1,294; xml: 45; ansic: 1
file content (174 lines) | stat: -rwxr-xr-x 5,240 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl -w
# SPDX-License-Identifier: AGPL-3.0-only
use strict;
use Net::LDAP;
use MIME::Base64;
use Data::Dumper;

sub readconfig($) {
	my ($fn) = @_;
	my %options;

	open(CFG, $fn) or die("unable to open ".$fn." config file");
	while (<CFG>) {
		if ($_ =~ /^\s*[#!]/) {
			next;
		}
		if ($_ =~ /^\s*(\S+)\s*=\s*([^\r]+)\r?$/) {
			my $idx = $1;
			my $val = $2;
			chomp($val);
			$val =~ s/\s+$//;
			$options{$idx} = $val;
		}
	}
	close(CFG);
	return %options;
}


my $servercfg = $ARGV[0];
$servercfg = "/etc/kopano/server.cfg" if (!defined($servercfg));

my %serveropt = readconfig($servercfg);

if (!defined($serveropt{user_plugin})) {
	print "First argument must be the server configuration file\n";
	exit(0);
}
if ($serveropt{user_plugin} ne "ldap" && $serveropt{user_plugin} ne "ldapms") {
	print "You only can change the unique attribute for the ldap user plugin, found: '".$serveropt{user_plugin}."'\n";
	exit(0);
}

my %ldapopt = readconfig($serveropt{user_plugin_config});

if (!defined($ldapopt{ldap_sendas_attribute_type}) && defined($ldapopt{ldap_user_sendas_attribute_type})) { 
	$ldapopt{ldap_sendas_attribute_type} = $ldapopt{ldap_user_sendas_attribute_type};
}

if (!defined($ldapopt{ldap_sendas_relation_attribute}) && defined($ldapopt{ldap_user_sendas_relation_attribute})) {
	$ldapopt{ldap_sendas_relation_attribute} = $ldapopt{ldap_user_sendas_relation_attribute};
}

if (!defined($ldapopt{ldap_sendas_attribute}) && defined($ldapopt{ldap_user_sendas_attribute})) {
	$ldapopt{ldap_sendas_attribute} = $ldapopt{ldap_user_sendas_attribute};
}

# sanity check
if (!defined($ldapopt{ldap_sendas_attribute}) || !defined($ldapopt{ldap_sendas_attribute_type}) || !defined($ldapopt{ldap_sendas_relation_attribute})) {
	print "Some of the required attributes are not set.\nPlease check the following values (or alias config names):\n";
	if (!defined($ldapopt{ldap_sendas_attribute})) {
		print "ldap_sendas_attribute\n";
	}
	if (!defined($ldapopt{ldap_sendas_attribute_type})) {
		print "ldap_sendas_attribute_type\n";
	}
	if (!defined($ldapopt{ldap_sendas_relation_attribute})) {
		print "ldap_sendas_relation_attribute\n";
	}
	exit(0);
}

my ($ldap_search_base, $ldap_search_filter);

if (!defined($ldapopt{ldap_search_base})) {
	print "Using pre 6.40 ldap config\n";
	$ldap_search_base = $ldapopt{ldap_user_search_base};
	$ldap_search_filter = $ldapopt{ldap_user_search_filter};
} else {
	print "Using post 6.40 ldap config\n";
	$ldap_search_base = $ldapopt{ldap_search_base};
	$ldap_search_filter = "(&(".$ldapopt{ldap_object_type_attribute}."=".$ldapopt{ldap_user_type_attribute_value}.")".$ldapopt{ldap_user_search_filter}.")";
	$ldapopt{ldap_user_scope} = "sub";
}

my $ldapuri = $ldapopt{ldap_protocol}."://".$ldapopt{ldap_host}.":".$ldapopt{ldap_port};
my $ldap = Net::LDAP->new($ldapuri) or die("LDAP connection failed");
my $msg = $ldap->bind($ldapopt{ldap_bind_user}, password => $ldapopt{ldap_bind_passwd}) or die ("LDAP bind failed");
$msg->code && die $msg->error;

my (%u2dn, %dn2u);
my $match = "dn";
if ($ldapopt{ldap_sendas_attribute_type} ne "dn") {
	print "Looking up all users...\n";
	if (defined($ldapopt{ldap_sendas_relation_attribute}) && $ldapopt{ldap_sendas_relation_attribute} ne "") {
		$match = $ldapopt{ldap_sendas_relation_attribute};
	} else {
		$match = $ldapopt{ldap_user_unique_attribute};
	}
	$match = lc($match);
	$msg = $ldap->search(base => $ldap_search_base, filter => $ldap_search_filter, scope => $ldapopt{ldap_user_scope}, attrs => [ $match ]);
	$msg->code && die $msg->error;
	my $users = $msg->as_struct();
	foreach (keys %$users) {
		my $dn = $_;
		my $value = $users->{$_}->{$match}[0];
		
		$u2dn{$value} = $dn;
		$dn2u{$dn} = $value;
	}
}

print "Finding send-as settings...\n";
$msg = $ldap->search(base => $ldap_search_base, filter => $ldap_search_filter, scope => $ldapopt{ldap_user_scope},
					 attrs => [$ldapopt{ldap_sendas_attribute}]);
$msg->code && die $msg->error;

my $users = $msg->as_struct();
my $attrUnique = lc($ldapopt{ldap_user_unique_attribute});
my $attrName = lc($ldapopt{ldap_loginname_attribute});
my $attrSendas = lc($ldapopt{ldap_sendas_attribute});

my %modify;
my %remove;

foreach (keys %$users) {
	my $dn = $_;
	my $valref = $$users{$_};
	my $unique = $valref->{$attrUnique}[0];
	my $username = $valref->{$attrName}[0];
	my $sendas = $valref->{$attrSendas};

	if (!defined($sendas)) {
		next;
	}

	foreach (@${sendas}) {
		print "remove $_ from $dn\n";
		push @{$remove{$dn}}, $_;

		if ($match ne "dn") {
			print "add ".$dn2u{$dn}." to ".$u2dn{$_}."\n";
			push @{$modify{$u2dn{$_}}}, $dn2u{$dn};
		} else {
			print "add $dn to $_\n";
			push @{$modify{$_}}, $dn;
		}
	}
}

print "\nApply above changes to ldap? [y] ";
my $ans = <STDIN>;
chomp($ans);
if (! ($ans eq "" || $ans =~ m/y.*/i)) {
	exit(0);
}

print "Removing old send-as settings...\n";
foreach (keys %remove) {
	my $dn = $_;

	$msg = $ldap->modify($dn, delete => [ $attrSendas ] );
	$msg->code && print $msg->error." for ".$dn."\n";
}

print "Setting new send-as settings...\n";
foreach (keys %modify) {
	my $dn = $_;

	$msg = $ldap->modify($dn, replace => { $attrSendas => [ @{$modify{$dn}} ] });
	$msg->code && print $msg->error." for ".$dn."\n";
}

$ldap->unbind();