-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkadmin-backend
More file actions
executable file
·1839 lines (1564 loc) · 67.7 KB
/
kadmin-backend
File metadata and controls
executable file
·1839 lines (1564 loc) · 67.7 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# kadmin-backend -- remctl interface to kadmin functionality.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Based heavily on work by Roland Schemers
# Copyright 2003, 2007, 2008, 2009, 2010, 2011, 2013, 2014
# The Board of Trustees of the Leland Stanford Junior University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
##############################################################################
# Modules and declarations
##############################################################################
use strict;
use Expect ();
use POSIX;
use Date::Parse;
# Disable sending of kadmin's output to our standard output.
$Expect::Log_Stdout = 0;
# Account used to test password strength.
our $STRENGTH = 'service/password-strength';
# Path to the ACL file of who can change passwords.
our $RESET_ACL = '/etc/remctl/acl/password-reset';
# Path to the blacklist file of additional people whose passwords may not be
# changed.
our $RESET_BLACKLIST = '/etc/kadmin/password-blacklist';
# Reserved principal names.
our %RESERVED = map { $_ => 1 } qw(admin kadmin krbtgt root service);
# Paths to various programs. By default, we search the current PATH.
our $K5_KADMIN = 'kadmin';
our $K5_KPASSWD = 'kpasswd';
our $K5START = 'k5start';
our $KASETKEY = 'kasetkey';
our $KSETPASS = 'ksetpass';
our $LDAPADD = 'ldapadd';
our $LDAPDELETE = 'ldapdelete';
our $LDAPMODIFY = 'ldapmodify';
our $LDAPSEARCH = 'ldapsearch';
# Per-instance configuration. Each key in this hash is an instance, with the
# empty string used for a null instance. Each value is a hash with the
# following key/value pairs:
#
# ad_config => OpenLDAP config file for AD LDAP commands
# ad_group => Group to which to add all accounts
# ad_keytab => Keytab containing credentials for AD authentication
# ad_ldif => Text::Template LDIF file used for AD account changes
# ad_realm => Kerberos realm for Active Directory
# ad_setpass => Use ksetpass rather than LDAP for password setting
# afs_admin => Principal for Kerberos v4 kasetkey authentication
# afs_fake => Whether to fake Kerberos v4 kadmin output
# afs_srvtab => Srvtab for Kerberos v4 kasetkey authentication
# acl => File listing principals that can manage this instance
# allowed => Regex matching permitted principal names (w/o instance)
# create_opts => Extra options to pass to kadmin addprinc
# k5_admin => Principal for Kerberos v5 kadmin authentication
# k5_host => Admin server for Kerberos v5 kadmin operations
# k5_keytab => Keytab for Kerberos v5 kadmin authentication
# locked => Program to check to see if we can enable an account
# policy => The password policy to set for created principals
# reset => True if we should allow password resets
#
# No instances are configured by default. In order for a particular instance
# (including the empty instance) to be managed by this program, configuration
# must be set for it. The presence of ad_config enables Active Directory
# propagation, afs_admin enables AFS kaserver, and k5_admin enables Kerberos
# v5 kadmin.
our %CONFIG = ();
# Load options from a configuration file, if present.
if (-r "/etc/kadmin-remctl.conf") {
do "/etc/kadmin-remctl.conf"
or die (($@ || $!) . "\n");
}
# The help text.
our $HELP = <<'EOH';
Kerberos administrative remctl help:
kadmin change_passwd <user> <old> <new> Change password for <user>
kadmin check_expire <user> expire|pwexpire Get account or pwd expire time
kadmin check_passwd <user> <password> Check strength of password
kadmin create <user> <pass> enabled|disabled Create <user> account
kadmin delete <user> Delete <user> account
kadmin disable <user> Disable <user> account
kadmin enable <user> Enable <user> account
kadmin examine <user> Show information for <user>
kadmin expiration <user> <date> Set expiration for <user>
kadmin instance check <user> <inst> Whether <user>/<inst> exists
kadmin instance create <user> <inst> <pass> Create <user>/<inst> account
kadmin instance delete <user> <inst> Delete <user>/<inst> account
kadmin instance list <inst> List all */<inst> accounts
kadmin instance reset <user> <inst> <pass> Set password for <user>/<inst>
kadmin pwexpiration <user> <date> Set expiration for <user>
kadmin reset_passwd <user> <password> Change password for <user>
EOH
##############################################################################
# Utility functions
##############################################################################
# Check whether a given principal is present in an ACL. Returns true if so,
# false otherwise. We handle remctl's include syntax and use a local hash to
# protect against including the same file twice.
sub check_acl_included {
my ($included, $acl, $principal) = @_;
open (ACL, '<', $acl) or die "error: cannot open $acl: $!\n";
local $_;
my $regex;
if ($principal =~ /\@/) {
$regex = qr/^\Q$principal\E\s/;
} else {
$regex = qr/^\Q$principal\E\@/;
}
while (<ACL>) {
if (/$regex/) {
return 1;
} elsif (/^\s*include\s+(\S+)/) {
die "error: recursive includes of $acl\n" if $included->{$1};
$included->{$1} = 1;
my $status = check_acl_included ($included, $1, $principal);
return 1 if $status;
}
}
close ACL;
return;
}
sub check_acl {
my %included;
return check_acl_included (\%included, @_);
}
# Check an instance and make sure it's one we're allowed to use. It must
# exist in the global instance hash and must be alphanumeric, and the account
# running this script must be permitted to manage it if an explicit ACL is
# set.
sub check_instance {
my ($instance) = @_;
unless ($CONFIG{$instance}) {
if ($instance) {
die "error: invalid instance: $instance\n";
} else {
die "error: cannot manage principals without an instance\n";
}
}
my $acl = $CONFIG{$instance}{acl};
die "error: REMOTE_USER not set" unless $ENV{REMOTE_USER};
if ($acl && !check_acl ($acl, $ENV{REMOTE_USER})) {
die "error: $ENV{REMOTE_USER} not allowed to manage $instance"
. " instances\n";
}
}
# Check a principal and make sure it's one that we're allowed to use.
sub check_principal {
my ($principal, $instance) = @_;
check_instance ($instance);
my $regex = $CONFIG{$instance}{allowed} || '^[a-z][0-9a-z]{1,7}\z';
if ($principal !~ /$regex/ || $RESERVED{$principal}) {
die "error: invalid principal: $principal\n";
}
}
# Check if we can use a password. We have to do a bit of sanity checking even
# though we're talking to Expect.
sub check_password {
my ($password) = @_;
if ($password =~ /[\x00-\x08\x0a-\x1f]/) {
die "error: invalid control characters in password\n";
}
}
##############################################################################
# Kerberos kadmin functions
##############################################################################
# Ensure that all of the necessary parameters for Kerberos v5 kadmin
# operations are set and return true if this instance does kaserver
# operations, false otherwise.
sub kadmin_config {
my ($instance) = @_;
return unless $CONFIG{$instance}{k5_admin};
die "error: no keytab configured for Kerberos v5 kadmin changes\n"
unless $CONFIG{$instance}{k5_keytab};
return 1;
}
# Spawn a conversation with k5admin and return an Expect object. Takes the
# instance to get the right configuration.
sub spawn_k5admin {
my ($instance) = @_;
my @args = ('-p', $CONFIG{$instance}{k5_admin}, '-k',
'-t', $CONFIG{$instance}{k5_keytab});
if ($CONFIG{$instance}{k5_host}) {
push (@args, '-s', $CONFIG{$instance}{k5_host});
}
my $k5admin = Expect->spawn ($K5_KADMIN, @args);
unless ($k5admin) {
die "error: cannot run $K5_KADMIN\n";
}
return $k5admin;
}
# Run a k5admin command and capture the output. Return a list consisting of
# the exit status and the output, or in a scalar context, just the exit
# status.
sub run_k5admin {
my ($instance, $command) = @_;
my @args = ('-p', $CONFIG{$instance}{k5_admin}, '-k',
'-t', $CONFIG{$instance}{k5_keytab}, '-q', $command);
if ($CONFIG{$instance}{k5_host}) {
push (@args, '-s', $CONFIG{$instance}{k5_host});
}
my $pid = open (K5ADMIN, '-|');
if (not defined $pid) {
die "error: cannot fork: $!\n";
} elsif ($pid == 0) {
open (STDERR, '>&STDOUT') or die "error: cannot dup stdout: $!\n";
exec ($K5_KADMIN, @args)
or die "error: cannot run $K5_KADMIN: $!\n";
}
local $_;
my @output;
while (<K5ADMIN>) {
push (@output, $_) unless /Authenticating as principal/;
}
close K5ADMIN;
my $status = ($? >> 8);
return wantarray ? ($status, join ('', @output)) : $status;
}
# Check whether a principal already exists in Kerberos. Returns false if it
# doesn't and true if it does.
sub kadmin_check {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my ($status, $output) = run_k5admin ($instance, "getprinc $principal");
return ($output =~ /does not exist/) ? 0 : 1;
}
# Create a new principal using kadmin. $status should be either enabled or
# disabled and controls the initial account status.
sub kadmin_create {
my ($principal, $instance, $password, $status) = @_;
check_principal ($principal, $instance);
check_password ($password);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my $command = 'add_principal +requires_preauth -allow_svr';
if ($CONFIG{$instance}{policy}) {
$command .= " -policy $CONFIG{$instance}{policy}";
} else {
$command .= ' -clearpolicy';
}
if ($status ne 'enabled') {
$command .= ' -allow_tix';
}
if ($CONFIG{$instance}{expiration}) {
my $expiration = time + $CONFIG{$instance}{expiration};
$expiration = strftime ('%Y-%m-%d %T', localtime $expiration);
$command .= ' -pwexpire "$expiration"';
}
if (exists $CONFIG{$instance}{create_opts}) {
$command .= ' ' . $CONFIG{$instance}{create_opts};
}
my $k5admin = spawn_k5admin ($instance);
unless ($k5admin->expect (2, 'kadmin:')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$command $principal\n");
unless ($k5admin->expect (2, 'password for principal')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$password\n");
unless ($k5admin->expect (2, 'password for principal')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$password\n");
my ($num, $error, $match, $before, $after)
= $k5admin->expect (30, -re => 'add_principal: .*\n', 'kadmin: ');
if ($num == 1) {
$match =~ s/^add_principal: //;
$match =~ s/\r?\n//;
warn "error: $match\n";
print "retstr: $match\n";
exit 1;
} elsif ($error) {
die "error: Expect said $error\n";
} else {
$k5admin->send ("quit\n");
$k5admin->soft_close;
}
}
# Delete a principal using kadmin.
sub kadmin_delete {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my ($status, $output)
= run_k5admin ($instance, "delete_principal -force $principal");
if ($status != 0 || $output =~ /^delete_principal: /) {
$output =~ s/^delete_principal: //;
$output =~ s/\r?\n.*//;
warn "error: $output\n";
print "retstr: $output\n";
exit 1;
}
}
# List all principals with a given instance using kadmin and return the
# results as a string.
sub kadmin_list {
my ($instance) = @_;
check_instance ($instance);
kadmin_config ($instance) or return '';
my ($status, $output)
= run_k5admin ($instance, "list_principals */$instance@*");
if ($status != 0 || $output =~ /^(get|list)_principals: /) {
$output =~ s/^(get|list)_principals: //;
$output =~ s/\r?\n.*//;
die "error: $output\n";
} else {
return $output;
}
}
# Disable a principal using kadmin.
sub kadmin_disable {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my ($status, $output)
= run_k5admin ($instance, "modprinc -allow_tix $principal");
if ($status != 0 || $output =~ /^modify_principal: /) {
$output =~ s/^modify_principal: //;
$output =~ s/\r?\n.*//;
warn "error: $output\n";
print "retstr: $output\n";
exit 1;
}
}
# Enable a principal using kadmin.
sub kadmin_enable {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
if (exists $CONFIG{$instance}{locked} && @{$CONFIG{$instance}{locked}}) {
my $retval = system (@{$CONFIG{$instance}{locked}}, $principal);
if ($retval == 0) {
warn "error: $principal is marked locked by external check\n";
print "retstr: $principal is marked locked by external check\n";
exit 1;
}
}
my ($status, $output)
= run_k5admin ($instance, "modprinc +allow_tix $principal");
if ($status != 0 || $output =~ /^modify_principal: /) {
$output =~ s/^modify_principal: //;
$output =~ s/\r?\n.*//;
warn "error: $output\n";
print "retstr: $output\n";
exit 1;
}
}
# Change a principal's expiration date using kadmin.
sub kadmin_expiration {
my ($principal, $instance, $expire) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my ($status, $output)
= run_k5admin ($instance, "modprinc -expire \"$expire\" $principal");
if ($status != 0 || $output =~ /^modify_principal: /) {
$output =~ s/^modify_principal: //;
$output =~ s/\r?\n.*//;
warn "error: $output\n";
print "retstr: $output\n";
exit 1;
}
}
# Change a principal's password expiration date using kadmin.
sub kadmin_pwexpiration {
my ($principal, $instance, $expire) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my ($status, $output)
= run_k5admin ($instance,
"modprinc -pwexpire \"$expire\" $principal");
if ($status != 0 || $output =~ /^modify_principal: /) {
$output =~ s/^modify_principal: //;
$output =~ s/\r?\n.*//;
warn "error: $output\n";
print "retstr: $output\n";
exit 1;
}
}
# Get a principal's expiration date or password expiration date using kadmin,
# as a UTC date in the format: YYYY-MM-DD HH:MM:SSZ (with Z a literal Z).
# Return '' if there is no expiration date set of the requested type.
sub kadmin_expiration_check {
my ($principal, $instance, $type) = @_;
check_principal ($principal, $instance);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my ($status, $output)
= run_k5admin ($instance, "getprinc $principal");
# Parse out the two possible expire times. We exit if we cannot find
# either, not checking to see if it the requested time -- that's
# potentially overkill, but does indicate that something's wrong.
my ($pwexpire, $expire) = (0, 0);
if ($output =~ /Password expiration date: (.*)/) {
$pwexpire = str2time ($1) unless $1 eq '[none]';
} else {
warn "error: could not find password expiration date\n";
exit 1;
}
if ($output =~ /Expiration date: (.*)/) {
$expire = str2time ($1) unless $1 eq '[never]';
} else {
warn "error: could not find expiration date\n";
exit 1;
}
# If no type was requested, return the soonest of the two dates.
if (!$type) {
$type = 'expire';
$type = 'pwexpire' if $pwexpire < $expire || $expire == 0;
}
if ($type eq 'pwexpire' && $pwexpire > 0) {
return strftime ("%Y-%m-%d %TZ", gmtime ($pwexpire));
} elsif ($type eq 'expire' && $expire > 0) {
return strftime ("%Y-%m-%d %TZ", gmtime ($expire));
}
return '';
}
# The K5 kadmin interface doesn't support checking the strength of a password
# without trying to change a password. We therefore test the strength of a
# password by changing the password of a designated special account (which is
# also set DISABLE_ALL_TIX) with the same password policy as our user accounts
# and seeing if the password is accepted.
#
# On success, do nothing. On failure, print the error message from K5 kadmin
# and exit with a non-zero status.
sub kadmin_validate {
my ($principal, $instance, $password) = @_;
check_password ($password);
my $k5admin = spawn_k5admin ($instance);
unless ($k5admin->expect (2, 'kadmin:')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("change_password $STRENGTH\n");
unless ($k5admin->expect (2, 'password for principal')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$password\n");
unless ($k5admin->expect (2, 'password for principal')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$password\n");
my ($num, $error, $match, $before, $after)
= $k5admin->expect (2, -re => 'change_password: .*\n', 'kadmin: ');
if ($num == 1) {
$match =~ s/^change_password: //;
$match =~ s/ while changing.*\n//;
warn "error: Insecure password rejected\n";
print "retstr: Insecure password: $match\n";
$k5admin->send ("quit\n");
$k5admin->soft_close;
exit 1;
} elsif ($error) {
die "error: Expect said $error\n";
} else {
$k5admin->send ("change_password -randkey $STRENGTH\n");
$k5admin->send ("quit\n");
$k5admin->soft_close;
}
}
# Reset a password via kadmin.
sub kadmin_reset {
my ($principal, $instance, $password) = @_;
check_principal ($principal, $instance);
check_password ($password);
kadmin_config ($instance) or return;
$principal = "$principal/$instance" if $instance;
my $k5admin = spawn_k5admin ($instance);
unless ($k5admin->expect (2, 'kadmin:')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("change_password $principal\n");
unless ($k5admin->expect (2, 'password for principal')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$password\n");
unless ($k5admin->expect (2, 'password for principal')) {
die "error: cannot talk to $K5_KADMIN\n";
}
$k5admin->send ("$password\n");
my ($num, $error, $match, $before, $after)
= $k5admin->expect (30, -re => 'change_password: .*\n', 'kadmin: ');
if ($num == 1) {
$match =~ s/^change_password: //;
$match =~ s/ while changing.*\n//;
warn "error: $match\n";
print "retstr: $match\n";
$k5admin->send ("quit\n");
$k5admin->soft_close;
exit 1;
} elsif ($error) {
die "error: Expect said $error\n";
} else {
$k5admin->send ("quit\n");
$k5admin->soft_close;
}
}
##############################################################################
# kpasswd functions
##############################################################################
# Change a password via kpasswd. This is always used for the change_passwd
# interface and we assume that kpasswd can do the right thing, since it works
# for both Active Directory and for MIT or Heimdal Kerberos.
sub kpasswd {
my ($principal, $instance, $old, $new) = @_;
check_principal ($principal, $instance);
check_password ($old);
check_password ($new);
$principal = "$principal/$instance" if $instance;
my $kpasswd = Expect->spawn ($K5_KPASSWD, $principal);
unless ($kpasswd) {
die "error: cannot run $K5_KPASSWD\n";
}
unless ($kpasswd->expect (2, 'Password for')) {
die "error: cannot talk to $K5_KPASSWD\n";
}
$kpasswd->send ($old . "\n");
my ($num, $error, $match, $before, $after)
= $kpasswd->expect (10, 'kpasswd: ', 'Enter new password: ');
if (defined ($num) && $num == 1) {
$after =~ s/\r?\n.*//s;
warn "error: $after\n";
print "retstr: $after\n";
exit 1;
} elsif ($error) {
die "error: Expect said $error\n";
}
$kpasswd->send ($new . "\n");
unless ($kpasswd->expect (2, 'Enter it again: ')) {
die "error: cannot talk to $K5_KPASSWD\n";
}
$kpasswd->send ($new . "\n");
($num, $error, $match, $before, $after)
= $kpasswd->expect (60, 'Password change rejected: ',
'Password changed.');
if (defined ($num) && $num == 1) {
$after =~ s/\..*//s;
$after =~ s/\r?\n/ /g;
$after =~ s/\s+See the kpasswd man page.*//s;
warn "error: $after\n";
print "retstr: $after\n";
exit 1;
} elsif ($error) {
die "error: Expect said $error\n";
} else {
$kpasswd->soft_close;
}
}
##############################################################################
# Active Directory functions
##############################################################################
# Check the configuration for Active Directory and make changes as needed for
# a particular instance. Does nothing if the ad_config attribute isn't set in
# the instance hash. Returns true if AD propagation is configured for that
# instance, false otherwise.
sub ad_config {
my ($instance) = @_;
return unless $CONFIG{$instance}{ad_config};
die "error: no LDIF configured for AD account changes\n"
unless $CONFIG{$instance}{ad_ldif};
die "error: no keytab configured for AD account changes\n"
unless $CONFIG{$instance}{ad_keytab};
my $config = $CONFIG{$instance}{ad_config};
if (!$ENV{LDAPCONF} || $ENV{LDAPCONF} ne $config) {
$ENV{LDAPCONF} = $config;
}
require Encode;
require MIME::Base64;
require Text::Template;
import Encode 'encode';
import MIME::Base64 'encode_base64';
return 1;
}
# Form an Active Directory command. Takes the instance and the command to run
# and wraps it with the necessary k5start bits for authentication. Requires
# the instance to find the right AD configuration.
sub ad_command {
my ($instance, @args) = @_;
my @command = ($K5START, '-Uqf', $CONFIG{$instance}{ad_keytab});
if ($CONFIG{$instance}{ad_realm}) {
push (@command, '-r', $CONFIG{$instance}{ad_realm});
}
push (@command, '--', @args);
return @command;
}
# Reset a password using ksetpass. Note that we don't have to check the
# password since we can set any password.
sub ksetpass {
my ($principal, $instance, $password) = @_;
check_principal ($principal, $instance);
if ($CONFIG{$instance}{ad_realm}) {
$principal .= '@' . $CONFIG{$instance}{ad_realm};
}
my @command = ad_command ($instance, $KSETPASS, $principal);
my $try = 1;
do {
sleep 1 if $try > 1;
my $pid = open (SETPASS, '|-', @command);
unless ($pid) {
die "error: cannot execute ksetpass: $!\n";
}
print SETPASS $password;
close SETPASS;
} while ($try++ < 5 && $? != 0);
if ($? != 0) {
warn "error: ksetpass of $principal failed\n";
return;
}
return 1;
}
# Determine the dn for a principal in AD, used for both adding those
# principals to groups and deleting them. In order to figure out the dn, we
# read the dn: line out of the LDIF file for this instance and then use
# Text::Template to build our DN.
sub ad_find_dn {
my ($principal, $instance) = @_;
my $source = $CONFIG{$instance}{ad_ldif};
open (SOURCE, '<', $source)
or die "error: cannot open $source: $!\n";
local $_;
my $dn;
while (<SOURCE>) {
next unless /^dn:\s+/;
chomp;
$dn = $_;
while (<SOURCE>) {
last unless /^\s/;
s/^\s+//;
$dn .= $_;
}
}
close SOURCE;
die "error: cannot determine account DN for delete\n" unless $dn;
$dn =~ s/^dn: //;
$dn =~ s/\s+$//;
my $template = Text::Template->new (TYPE => 'STRING', SOURCE => $dn)
or die "error: cannot build DN template: $Text::Template::ERROR\n";
my %vars = (principal => $principal, instance => $instance);
$dn = $template->fill_in (HASH => \%vars);
unless (defined $dn) {
die "error: cannot create DN: $Text::Template::ERROR\n";
}
return $dn;
}
# Check whether an account already exists in Active Directory. Takes the
# principal and the instance and returns true if the user exists, false
# otherwise.
sub ad_ldap_exists {
my ($principal, $instance) = @_;
if ($principal =~ /[\'\\]/) {
die "error: invalid user name $principal\n";
}
$principal = "$principal.$instance" if $instance;
ad_config ($instance) or return;
my @command = ad_command ($instance, $LDAPSEARCH, '-Q', '-LLL');
my $output = `@command 'samaccountname=$principal'`;
return ($output ne '') ? 1 : 0;
}
# Add an account to an Active Directory authorization group.
sub ad_group_add {
my ($principal, $instance) = @_;
ad_config ($instance) or return;
my $dn = ad_find_dn ($principal, $instance);
my $group = $CONFIG{$instance}{ad_group} or return;
my @command = ad_command ($instance, $LDAPMODIFY, '-Q');
my $pid = open (MODIFY, '|-', @command);
unless ($pid) {
die "error: cannot execute ldapmodify: $!\n";
}
print MODIFY "dn: $group\n";
print MODIFY "changetype: modify\n";
print MODIFY "add: member\n";
print MODIFY "member: $dn\n";
print MODIFY "-\n";
close MODIFY;
if ($? != 0) {
die "error: ldapmodify of account in AD failed: $?\n";
}
}
# Create a new account in Active Directory by instantiating the Text::Template
# template to create the LDIF and then passing that to ldapadd.
sub ad_ldap_create {
my ($principal, $instance, $password, $status) = @_;
check_principal ($principal, $instance);
check_password ($password);
ad_config ($instance) or return;
my $source = $CONFIG{$instance}{ad_ldif};
my $template = Text::Template->new (TYPE => 'FILE', SOURCE => $source)
or die "error: could not create LDIF: $Text::Template::ERROR\n";
my $b64pass = encode_base64 (encode ('ucs-2le', qq{"$password"}));
chomp $b64pass;
my $control = ($status eq 'enabled' ? 512 : 514);
if ($CONFIG{$instance}{ad_setpass} && $control == 512) {
$control = 514;
}
my %vars = (principal => $principal,
instance => $instance,
password => $b64pass,
control => $control);
my $result = $template->fill_in (HASH => \%vars);
unless (defined $result) {
die "error: could not create LDIF: $Text::Template::ERROR\n";
}
my @command = ad_command ($instance, $LDAPADD, '-Q');
my $pid = open (ADD, '|-', @command);
unless ($pid) {
die "error: cannot execute ldapadd: $!\n";
}
print ADD $result;
close ADD;
if ($? != 0) {
die "error: ldapadd of account to AD failed: $?\n";
}
if ($CONFIG{$instance}{ad_setpass}) {
unless (ksetpass ($principal, $instance, $password)) {
ad_ldap_delete ($principal, $instance);
my $full = $principal;
$full .= "/$instance" if $instance;
die "error: ksetpass for $full failed\n";
}
if ($status eq 'enabled') {
ad_ldap_enable ($principal, $instance);
}
}
if ($CONFIG{$instance}{ad_group}) {
ad_group_add ($principal, $instance);
}
}
# Delete a user account out of Active Directory. Takes the principal and
# instance.
sub ad_ldap_delete {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
ad_config ($instance) or return;
my $dn = ad_find_dn ($principal, $instance);
my @command = ad_command ($instance, $LDAPDELETE, '-Q');
system (@command, $dn) == 0
or die "error: ldapdelete of account in AD failed\n";
}
# Enable an account in Active Directory by setting the userAccountControl to
# 512. We don't currently handle any other flags.
sub ad_ldap_enable {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
ad_config ($instance) or return;
my $dn = ad_find_dn ($principal, $instance);
my @command = ad_command ($instance, $LDAPMODIFY, '-Q');
my $pid = open (MODIFY, '|-', @command);
unless ($pid) {
die "error: cannot execute ldapmodify: $!\n";
}
print MODIFY "dn: $dn\n";
print MODIFY "changetype: modify\n";
print MODIFY "replace: userAccountcontrol\n";
print MODIFY "userAccountControl: 512\n";
close MODIFY;
if ($? != 0) {
die "error: ldapmodify to enable account failed\n";
}
}
# Disable an account in Active Directory by setting the userAccountControl to
# 514. We don't currently handle any other flags.
sub ad_ldap_disable {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
ad_config ($instance) or return;
my $dn = ad_find_dn ($principal, $instance);
my @command = ad_command ($instance, $LDAPMODIFY, '-Q');
my $pid = open (MODIFY, '|-', @command);
unless ($pid) {
die "error: cannot execute ldapmodify: $!\n";
}
print MODIFY "dn: $dn\n";
print MODIFY "changetype: modify\n";
print MODIFY "replace: userAccountcontrol\n";
print MODIFY "userAccountControl: 514\n";
close MODIFY;
if ($? != 0) {
die "error: ldapmodify to disable account failed\n";
}
}
# Reset a password in Active Directory using ksetpass.
sub ad_reset {
my ($principal, $instance, $password) = @_;
check_principal ($principal, $instance);
ad_config ($instance) or return;
ksetpass ($principal, $instance, $password) or exit 1;
}
##############################################################################
# Kerberos v4 kadmin and AFS kaserver functions
##############################################################################
# Ensure that all of the necessary parameters for AFS kaserver manipulation
# are set and return true if this instance does kaserver operations, false
# otherwise.
sub kaserver_config {
my ($instance) = @_;
return unless $CONFIG{$instance}{afs_admin};
die "error: no srvtab configured for AFS kaserver changes\n"
unless $CONFIG{$instance}{afs_srvtab};
return 1;
}
# Run a kasetkey command and return the exit status. Takes the instance as
# the first parameter to find the right configuration and returns a list
# consisting of the exit status and the output, or in a scalar context, just
# the exit status.
sub run_kasetkey {
my ($instance, @args) = @_;
my @command = ($KASETKEY, '-k', $CONFIG{$instance}{afs_srvtab},
'-a', $CONFIG{$instance}{afs_admin}, @args);
my $pid = open (KASETKEY, '-|');
if (not defined $pid) {
die "error: cannot fork: $!\n";
} elsif ($pid == 0) {
open (STDERR, '>&STDOUT') or die "error: cannot dup stdout: $!\n";
exec (@command)
or die "error: cannot run $KASETKEY: $!\n";
}
local $_;
my @output;
push (@output, $_) while <KASETKEY>;
close KASETKEY;
my $status = ($? >> 8);
return wantarray ? ($status, join ('', @output)) : $status;
}
# Create a new Kerberos v4 account with a random password and set its status.
# We assume that the creation of the account elsewhere will reset the password
# in Kerberos v4.
sub kaserver_create {
my ($principal, $instance, $password, $status) = @_;
check_principal ($principal, $instance);
check_password ($password);
kaserver_config ($instance) or return;
$principal = "$principal.$instance" if $instance;
my ($code, $output) = run_kasetkey ($instance, '-r', '-s', $principal);
if ($code != 0) {
$output =~ s/\n.*//;
die "error: cannot create K4 principal for $principal: $output\n";
}
if ($status ne 'enabled') {
($code, $output) = run_kasetkey ($instance, '-n', '-s', $principal);
if ($code != 0) {
$output =~ s/\n.*//;
die "error: cannot disable K4 principal for $principal: $output\n";
}
}
}
# Delete a Kerberos v4 principal.
sub kaserver_delete {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kaserver_config ($instance) or return;
$principal = "$principal.$instance" if $instance;
my ($status, $output) = run_kasetkey ($instance, '-D', $principal);
if ($status != 0) {
$output =~ s/\n.*//;
die "error: cannot delete $principal in Kerberos v4: $output\n";
}
}
# Disable a Kerberos v4 principal.
sub kaserver_disable {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kaserver_config ($instance) or return;
$principal = "$principal.$instance" if $instance;
my ($status, $output) = run_kasetkey ($instance, '-n', '-s', $principal);
if ($status != 0) {
$output =~ s/\n.*//;
die "error: cannot disable $principal in Kerberos v4: $output\n";
}
}
# Enable a Kerberos v4 principal.
sub kaserver_enable {
my ($principal, $instance) = @_;
check_principal ($principal, $instance);
kaserver_config ($instance) or return;
$principal = "$principal.$instance" if $instance;
my ($status, $output) = run_kasetkey ($instance, '-t', '-s', $principal);
if ($status != 0) {
$output =~ s/\n.*//;
die "error: cannot enable $principal in Kerberos v4: $output\n";
}
}
##############################################################################
# Password changes and strength checking
##############################################################################
# Reset a password. The only tricky part here is that we have to be sure that
# we're not resetting the password of a privileged account. No user who can
# themselves reset passwords is allowed to have their password changed by this
# interface, and there may also be a separate blacklist of accounts. So
# first, we have to validate that.
sub reset_password {
my ($principal, $instance, $password) = @_;
check_principal ($principal, $instance);
check_password ($password);
unless ($CONFIG{$instance}{reset}) {
die "error: password reset not permitted for $instance instances\n";
}
my $full = $principal;
$full .= "/$instance" if $instance;
if (check_acl ($RESET_ACL, $full)) {
warn "error: password changes not permitted for that user\n";
exit 2;
}
if (-f $RESET_BLACKLIST && check_acl ($RESET_BLACKLIST, $full)) {
warn "error: password changes not permitted for that user\n";
exit 2;
}
if ($CONFIG{$instance}{k5_admin}) {
kadmin_reset ($principal, $instance, $password);
} elsif ($CONFIG{$instance}{ad_config}) {
ad_reset ($principal, $instance, $password);
}
}
# Change a user's password given the old password. We do this by spawning
# kpasswd and talking to it via Expect since that's the easiest way to make
# sure that we've validated the old password and everything is working
# properly. Currently, we don't do anything here except call kpasswd and
# assume that any further propagation is handled on the server side.
sub change_password {