3 # Copyright (c) 2010 Peter Palfrader
5 # Resets the password for a kerberos principal given on the command line.
6 # If the principal does not exist, try to create them.
14 my $USAGE = "Usage: $PROGRAM_NAME [--admin=<admin>] [--keytab=<file>] <principal>\n";
17 my $username = getpwuid($UID);
18 die "Cannot get current username\n" unless defined $username;
23 Getopt::Long::config('bundling');
25 '--help' => \$params->{'help'},
26 '--admin=s' => \$params->{'admin'},
27 '--keytab=s' => \$params->{'keytab'},
30 if ($params->{'help'}) {
35 die $USAGE if (scalar @ARGV != 1);
36 my $name = shift @ARGV;
38 unless (defined $params->{'admin'}) {
39 $params->{'admin'} = getname().'/admin';
41 unless (defined $params->{'keytab'}) {
42 $params->{'keytab'} = '/etc/userdir-ldap/keytab.'.getname();
45 my $client = Heimdal::Kadm5::Client->new(
46 Principal => $params->{'admin'},
47 Keytab => $params->{'keytab'}
49 die "Unable to get Heimdal Client object.\n" unless defined $client;
52 my $password = '844u6MrG0gTS';
54 my $rnd = new String::Random;
55 my $password = $rnd->randregex('[a-zA-Z0-9]{16}');
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";
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";