From 7c2dc3d833206c9f869a4bf55db6c69c37953d7f Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Fri, 31 Dec 2010 04:05:20 +0100 Subject: adduser also creates maildir --- INSTALL | 7 +- default.yml | 6 +- fripost-adduser.pl | 129 ++++++++++++++++++++++------------- lib/Fripost/Schema/Result/Mailbox.pm | 6 +- 4 files changed, 92 insertions(+), 56 deletions(-) diff --git a/INSTALL b/INSTALL index 8363fb3..356d726 100644 --- a/INSTALL +++ b/INSTALL @@ -1,6 +1,7 @@ You need several cpan modules to use these scripts. -If you use Debian GNU/Linux, you may install them like so: +If you use Debian GNU/Linux, you can install them like so: -apt-get install libdatetime-format-mysql-perl libdatetime-perl \ -libdbix-class-perl libemail-valid-perl libio-prompt-perl libyaml-syck-perl +aptitude install -R libdatetime-format-mysql-perl libdatetime-perl \ +libdbix-class-perl libemail-valid-perl libio-prompt-perl libstring-mkpasswd-perl \ +libyaml-syck-perl diff --git a/default.yml b/default.yml index 62e6efb..3f36fdf 100644 --- a/default.yml +++ b/default.yml @@ -1,3 +1,7 @@ # default.yml -- defaults for fripost administrative scripts --- -dbi_dsn: dbi:mysql:mail;host=127.0.0.1;port=2000 +dbi_dsn: dbi:mysql:mail;host=127.0.0.1;port=3306 +maildir_base: /home/mail/virtual/ +maildir_user: postfix +maildir_group: postfix +maildir_umask: 0700 diff --git a/fripost-adduser.pl b/fripost-adduser.pl index 4cf2040..d7a0fd3 100755 --- a/fripost-adduser.pl +++ b/fripost-adduser.pl @@ -35,6 +35,68 @@ use Getopt::Long; use String::MkPasswd qw/mkpasswd/; use YAML::Syck; +# Prompt for user info +sub read_user_info { + my %user; + # Get the full e-mail of the user (aka e-mail) + while (not defined $user{username}) { + $user{username} = prompt "New username: "; + if (!($user{username} =~ /\@/)) { + $user{username} .= '@fripost.org'; + say "Using $user{username}"; + } + if (!Email::Valid->address($user{username})) { + undef $user{username}; + say "This is not a valid e-mail address. Try again." + } + } + + # Full name of user + $user{name} = prompt "Full (real) name: "; + + # Extrapolate domain from full e-mail + my @parts = split /\@/, $user{username}; + my $username = $parts[0]; + my $domain = $parts[1]; + + # Set domain name + $user{domain} = $domain; + + # Construct maildir from domain and user + $user{maildir} = "$domain/$username/Maildir"; + + # Set dates + my $now = DateTime->now( + # locale => 'sv_SE', + # time_zone => 'Europe/Stockholm', + ); + $user{create_date} = $now; + $user{change_date} = $now; + + $user{active} = 1; + + # Generate password + my $password = mkpasswd( + -length => 20, + -minnum => 5, + -minspecial => 3 + ); + $user{password} = smd5($password); + + # Show the information that will be inserted + say Dumper \%user; + say "Generated password: $password"; + + # Ask the user if the information is OK + my $confirmed = prompt "Is this OK? ", -yn; + + if ($confirmed) { + return \%user; + } else { + return undef; + } +} + ## Get command line options our $conf = LoadFile('default.yml'); @@ -49,63 +111,32 @@ my $schema = Fripost::Schema->connect( $conf->{dbi_dsn}, $conf->{admuser}, $conf->{admpass}, {} #\%dbi_params ); -my %user; - say "Adding a new virtual user."; -# Get the full e-mail of the user (aka e-mail) -while (not defined $user{username}) { - $user{username} = prompt "New username: "; - if (!Email::Valid->address($user{username})) { - undef $user{username}; - say "This is not a valid e-mail address. Try again." - } -} - -# Full name of user -$user{name} = prompt "Full (real) name: "; +my $user = read_user_info(); -# Extrapolate domain from full e-mail -my @parts = split /\@/, $user{username}; -my $username = $parts[0]; -my $domain = $parts[1]; +if (!defined $user) { + say "Aborted by user."; + exit 1; +} -# Set domain name -$user{domain} = $domain; +## Create maildir +my ($login,$pass,$uid,$gid) = getpwnam($conf->{maildir_user}) + or die "maildir_user not found: $conf->{maildir_user}"; -# Construct maildir from domain and user -$user{maildir} = "$domain/$username/Maildir"; +my $maildir_loc = $conf->{maildir_base} . '/' . $user->{maildir}; -# Set dates -my $now = DateTime->now(); -$user{create_date} = $now; -$user{change_date} = $now; +system(qw/sudo mkdir -p -m/, $conf->{maildir_umask}, $maildir_loc =~ m!(.+)/Maildir$!); +system(qw/sudo maildirmake/, $maildir_loc); +system(qw/sudo chmod/, $conf->{maildir_umask}, $maildir_loc); +system(qw/sudo chown -R/, "$conf->{maildir_user}:$conf->{maildir_group}", $conf->{maildir_base}); -$user{active} = 1; +say "Created maildir in $maildir_loc"; -# Generate password -my $password = mkpasswd( - -length => 20, - -minnum => 5, - -minspecial => 3 -); -$user{password} = smd5($password); - -# Show the information that will be inserted -say Dumper \%user; -say "Generated password: "; -say $password; - -# Make the insert after a prompt -my $ok = prompt "Is this OK? ", -yn; - -if (!$ok) { - say "Aborted by user." -} else { - my $user = $schema->resultset('Mailbox')->new(\%user); - $user->insert; - say "New account added." -} +## Insert user into database +my $db_user = $schema->resultset('Mailbox')->new($user); +$db_user->insert; +say "New account $user->{username} added."; =head1 AUTHOR diff --git a/lib/Fripost/Schema/Result/Mailbox.pm b/lib/Fripost/Schema/Result/Mailbox.pm index 577c350..0932d5c 100644 --- a/lib/Fripost/Schema/Result/Mailbox.pm +++ b/lib/Fripost/Schema/Result/Mailbox.pm @@ -26,10 +26,10 @@ __PACKAGE__->load_components(qw/InflateColumn::DateTime/); __PACKAGE__->table('mailbox'); __PACKAGE__->add_columns(qw/ username password name maildir domain active /); __PACKAGE__->add_columns( - create_date => { data_type => 'datetime', timezone => "Europe/Stockholm", locale => "se_SV" }, - change_date => { data_type => 'datetime', timezone => "Europe/Stockholm", locale => "se_SV" } + create_date => { data_type => 'datetime', timezone => "Europe/Stockholm", locale => 'sv_SE' }, + change_date => { data_type => 'datetime', timezone => "Europe/Stockholm", locale => 'sv_SE' } ); - + __PACKAGE__->set_primary_key('username'); =head1 NAME -- cgit v1.2.3