ud-mailgate: remove exception for münchen.debian.net
[mirror/userdir-ldap.git] / ud-krb-reset
1 #!/usr/bin/perl
2
3 # Copyright (c) 2010 Peter Palfrader
4
5 # Resets the password for a kerberos principal given on the command line.
6 # If the principal does not exist, try to create them.
7
8 use strict;
9 use Heimdal::Kadm5;
10 use Getopt::Long;
11 use English;
12 use String::Random;
13
14 my $USAGE = "Usage: $PROGRAM_NAME [--admin=<admin>] [--keytab=<file>] <principal>\n";
15
16 sub getname() {
17         my $username = getpwuid($UID);
18         die "Cannot get current username\n" unless defined $username;
19         return $username;
20 };
21
22 my $params;
23 Getopt::Long::config('bundling');
24 GetOptions (
25         '--help'        => \$params->{'help'},
26         '--admin=s'     => \$params->{'admin'},
27         '--keytab=s'    => \$params->{'keytab'},
28 ) or die ($USAGE);
29
30 if ($params->{'help'}) {
31         print $USAGE;
32         exit (0);
33 };
34
35 die $USAGE if (scalar @ARGV != 1);
36 my $name = shift @ARGV;
37
38 unless (defined $params->{'admin'}) {
39         $params->{'admin'} = getname().'/admin';
40 };
41 unless (defined $params->{'keytab'}) {
42         $params->{'keytab'} = '/etc/userdir-ldap/keytab.'.getname();
43 };
44
45 my $client = Heimdal::Kadm5::Client->new(
46         Principal => $params->{'admin'},
47         Keytab => $params->{'keytab'}
48         );
49 die "Unable to get Heimdal Client object.\n" unless defined $client;
50
51
52 my $password = '844u6MrG0gTS';
53
54 my $rnd = new String::Random;
55 my $password = $rnd->randregex('[a-zA-Z0-9]{16}');
56
57 my $principal = $client->getPrincipal($name);
58 unless (defined $principal) {
59         print "Principal appears to not exist.  Trying to add.\n";
60         $principal = $client->makePrincipal($name);
61         my $ret = $client->createPrincipal($principal, $password, undef);
62         die "Failed to create principal $name.\n" unless ($ret);
63         print "Created principal $name with password '$password'.\n";
64 } else {
65         my $ret = $client->changePassword($name, $password);
66         die "Failed to change password for $name.\n" unless ($ret);
67         print "Changed password of principal $name to '$password'.\n";
68 };