aboutsummaryrefslogtreecommitdiffstats
path: root/addadmin.pl
blob: 508d100f3895e890d769576dca4048c21f12d1d9 (plain)
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
#!/usr/bin/perl

# Run `sudo ./addadmin.pl' to add yourself as an administrator for virtual
# mail hosting.
# (Use the optional argument if you're not happy with your login name.)
#
# To use the tools, you'll need to edit `~/.fripost.yml' and replace
# `bind_dn' and `bind_pw' by, respectively, the returned Distinguished Name
# and your password

use 5.010_000;
use strict;
use warnings;
use utf8;

use FindBin qw($Bin);
use lib "$Bin/lib";

use Fripost::Schema;
use Fripost::Password;
use Fripost::Prompt;
use YAML::Syck;

my $user = $ARGV[0];
$user //= $ENV{SUDO_USER};
$user //= $ENV{USER};

die "Error: Cannot find user name.\n"
    unless defined $user;

# Connect to the LDAP server
my $ldapconf = LoadFile ( 'ldap.yml' );
my $ldap = Fripost::Schema->new( $ldapconf );
$ldap = $ldap->{_ldap};


my $dn = join ',', ( 'cn='.$user
                   , 'ou=managers'
                   , (split ',', $ldapconf->{base_dn},2)[1] );

my $password = hash( undef, undef, prompt_password() );
my $res = $ldap->add( $dn,
                attrs => [ objectClass  => [ 'simpleSecurityObject'
                                           , 'organizationalRole' ]
                         , userPassword => $password
                         ]
              );
    die "Error: " .$res->error. "\n" if $res->code;

say $dn;
$ldap->unbind();