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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
#!/usr/bin/env perl
# vim: ts=4:sw=4:et
use warnings;
use strict;
use Authen::SASL;
use Getopt::Long qw(:config bundling no_ignore_case);
use JSON;
use MIME::Base64;
use Net::LDAP;
use Net::LDAP::Constant qw(
LDAP_CONTROL_ASSERTION
LDAP_CONTROL_POSTREAD
LDAP_FEATURE_MODIFY_INCREMENT
);
use Net::LDAP::Control::Assertion;
use Net::LDAP::Control::PostRead;
use Net::LDAP::Util qw(
escape_dn_value
escape_filter_value
time_to_generalizedTime
generalizedTime_to_time
);
use Time::HiRes qw(usleep);
sub _debug { warn "debug: @_\n" if $ENV{DEBUG}; }
sub _die { die "fatal: @_\n"; }
sub ldap_read_attr {
my ($conn, $dn, $attr) = @_;
my $res;
$res = $conn->search(base => $dn,
scope => "base",
filter => "(objectClass=*)",
attrs => [$attr]);
ldap_check($res);
if ($res->count > 0) {
return $res->entry(0)->get_value($attr);
} else {
return undef;
}
}
sub ldap_cas_attr {
my ($conn, $dn, $attr, $old, $new) = @_;
my $control = [];
my $res;
if ($old eq $new) {
_debug("ignoring no-op update '$old' -> '$old'");
return 1;
}
# optimization: RFC 4528 Assertion
# (not sure if useful; perhaps if used with 'replace'?)
if ($conn->root_dse->supported_control(LDAP_CONTROL_ASSERTION)) {
_debug("using Assertion control");
$control = [
Net::LDAP::Control::Assertion->new("($attr=$old)"),
];
}
# atomically delete old value and add the new one
#
# (NOTE: actually not sure if LDAP *guarantees* atomicity within the same
# Modify op, but if all clients cooperate, this should be good enough)
$res = $conn->modify($dn,
changes => [
delete => [ $attr => $old ],
add => [ $attr => $new ],
],
control => $control,
);
ldap_check($res, $dn, ["LDAP_NO_SUCH_ATTRIBUTE", "LDAP_ASSERTION_FAILED"]);
return !$res->is_error;
}
sub ldap_increment_attr {
my ($conn, $dn, $attr, $incr) = @_;
$incr ||= 1;
my $res;
# optimization: RFC 4525 Modify-Increment + RFC 4527 Post-Read
if ($conn->root_dse->supported_control(LDAP_CONTROL_POSTREAD)
&& $conn->root_dse->supported_feature(LDAP_FEATURE_MODIFY_INCREMENT))
{
_debug("using Modify-Increment extension");
$res = $conn->modify($dn,
increment => { $attr => $incr },
control => [ Net::LDAP::Control::PostRead->new(attrs => [$attr]) ],
);
ldap_check($res);
if ($res->control(LDAP_CONTROL_POSTREAD)) {
return $res->control(LDAP_CONTROL_POSTREAD)->entry->get_value($attr);
}
}
# manual compare-and-swap
my $val;
my $done = 0;
my $wait = 0;
_debug("unable to use Modify-Increment; using modify loop");
until ($done) {
_debug("fetching $attr");
$val = ldap_read_attr($conn, $dn, $attr);
_debug("fetched '$val', swapping");
$done = ldap_cas_attr($conn, $dn, $attr, $val, $val+$incr);
_debug($done ? "finished" : "retrying");
if (!$done) {
usleep(0.05 * 2**int(rand(++$wait)));
}
}
return $val+$incr;
}
sub ldap_format_error {
my ($res, $dn) = @_;
my $text = "LDAP error: ".$res->error;
utf8::decode($text);
$text .= "\n * error code: ".$res->error_name if $::debug;
$text .= "\n * failed entry: ".$dn if $dn;
$text .= "\n * matched entry: ".$res->dn if $res->dn;
my $i = 1;
while ($::debug) {
my ($pkg, $file, $line, $subr) = caller($i++);
if (!$pkg) {
last;
}
$text .= "\n * stack: $pkg | $file:$line | $subr";
}
return $text;
}
sub ldap_check {
my ($res, $dn, $ignore) = @_;
if (!$res->is_error) {
return 1;
}
utf8::decode($dn) if $dn;
if (ref $ignore eq 'ARRAY' && grep {$res->error_name eq $_} @$ignore) {
return 0;
}
my $text = ldap_format_error($res, $dn);
_die($text);
}
sub read_file {
my ($path) = @_;
my $buf;
if (open(my $fh, "<", $path)) {
$fh->read($buf, 64*1024);
close($fh);
return $buf;
} else {
_die("could not open '$path': $!");
}
}
sub encode_pem {
my ($buf) = @_;
my $type = "CERTIFICATE";
return "-----BEGIN $type-----\n"
.encode_base64($buf)
."-----END $type-----\n";
}
sub decode_pem {
my ($buf) = @_;
if ($buf =~ /^-----BEGIN .+?-----\n(.+?)\n-----END .+?-----$/ms) {
return decode_base64($1);
}
}
my $action;
my %opt = (
uri => undef,
dn => undef,
mech => "GSSAPI",
serial => undef,
attr => undef,
value => undef,
);
GetOptions(
"A|action=s" => \$action,
"H|uri=s" => \$opt{uri},
"b|dn=s" => \$opt{dn},
"Y|mech=s" => \$opt{mech},
"s|serial=s" => \$opt{serial},
"a|attr=s" => \$opt{attr},
"n|value=s" => \$opt{value},
) or exit(2);
$action or _die("action not specified");
$opt{uri} or _die("LDAP server URI not specified");
$opt{dn} or _die("LDAP base DN not specified");
_debug("connecting to '$opt{uri}'");
my $conn = Net::LDAP->new($opt{uri}) or die "$@";
my $sasl = Authen::SASL->new(mechanism => $opt{mech});
_debug("authenticating via SASL '$opt{mech}'");
ldap_check($conn->bind(sasl => $sasl));
if ($action eq "ping") {
exit;
}
if ($action eq "increment") {
$opt{attr} or _die("LDAP attribute name not specified");
$opt{value} //= 1;
_debug("incrementing attr '$opt{attr}' of entry '$opt{dn}'");
my $value = ldap_increment_attr($conn, $opt{dn}, $opt{attr}, $opt{value});
_debug("received value '$value'");
print "$value\n";
exit;
}
if ($action eq "publish-cert") {
$opt{serial} or _die("serial number not specified");
$opt{value} or _die("file name not specified");
my $cert = read_file($opt{value});
if ($cert =~ /^-----/) {
$cert = decode_pem($cert);
}
my $dn = "serialNumber=".escape_dn_value($opt{serial}).",".$opt{dn};
_debug("creating entry '$dn'");
my $res = $conn->add($dn, attrs => [
"objectClass" => ["pkiCertificate"],
"userCertificate;binary" => [$cert],
]);
ldap_check($res);
exit;
}
if ($action eq "publish-crl") {
$opt{value} or _die("file name not specified");
my $crl = read_file($opt{value});
if ($crl =~ /^-----/) {
$crl = decode_pem($crl);
}
_debug("updating entry '$opt{dn}'");
my $res = $conn->modify($opt{dn}, replace => {
"certificateRevocationList;binary" => [$crl],
});
ldap_check($res);
exit;
}
if ($action eq "revoke") {
$opt{serial} or _die("serial number not specified");
$opt{value} //= int time();
my $serial = escape_filter_value($opt{serial});
my $time = time_to_generalizedTime($opt{value});
_debug("searching for certificates");
my $res = $conn->search(base => $opt{dn},
filter => "(&(objectClass=pkiCertificate)(serialNumber=$serial))",
attrs => ["revocationTime"]);
ldap_check($res);
if ($res->count < 1) {
_die("could not find any certificate with serialNumber=$serial");
}
if ($res->count > 1) {
_die("found more than one certificate with serialNumber=$serial");
}
for my $entry ($res->entries) {
_debug("updating entry '".$entry->dn."'");
my $res = $conn->modify($entry->dn, add => {revocationTime => $time});
ldap_check($res);
}
exit;
}
if ($action eq "list-revoked") {
_debug("searching for revoked certificates");
my $res = $conn->search(base => $opt{dn},
filter => "(&(objectClass=pkiCertificate)(revocationTime=*))",
attrs => ["*"]);
my $output = [];
for my $entry ($res->entries) {
my $dn = $entry->dn;
my $serial = $entry->get_value("serialNumber");
if (!$serial || $serial !~ /^[0-9]+$/) {
warn "entry '$dn' has bad serialNumber '$serial'\n";
next;
}
my $cert = $entry->get_value("userCertificate;binary");
if (!$cert) {
warn "entry '$dn' is missing userCertificate;binary\n";
next;
}
my $revoked = $entry->get_value("revocationTime");
my $revoked_tm = generalizedTime_to_time($revoked);
if (!$revoked || !$revoked_tm) {
warn "entry '$dn' has bad revocationTime '$revoked'\n";
}
push @$output, {dn => $entry->dn,
serial => $serial,
cert => encode_pem($cert),
revoked => int $revoked_tm};
}
print JSON->new->encode($output);
exit;
}
_die("unknown mode '$action'");
|