diff options
-rw-r--r-- | INSTALL | 5 | ||||
-rwxr-xr-x | fripost-adduser | 141 | ||||
-rwxr-xr-x | fripost-newalias | 4 | ||||
-rwxr-xr-x | fripost-passwd | 21 | ||||
-rwxr-xr-x | lib/Fripost/Logger.pm | 3 | ||||
-rwxr-xr-x | lib/Fripost/Password.pm | 4 | ||||
-rwxr-xr-x | lib/Fripost/Prompt.pm | 75 | ||||
-rwxr-xr-x | lib/Fripost/Schema.pm | 1 | ||||
-rw-r--r-- | lib/Fripost/Schema/Result/Mailbox.pm | 18 | ||||
-rw-r--r-- | templ/new_user_mail.tt | 4 | ||||
-rw-r--r-- | templ/user_info.tt | 28 |
11 files changed, 202 insertions, 102 deletions
@@ -2,6 +2,7 @@ You need several cpan modules to use these scripts. If you use Debian GNU/Linux, you can install them like so: -aptitude install -R libdatetime-format-mysql-perl libdatetime-perl \ +sudo aptitude install -R libdatetime-format-mysql-perl libdatetime-perl \ libdbix-class-perl libemail-valid-perl libfile-slurp-perl libio-prompt-perl \ -libmime-lite-perl libstring-mkpasswd-perl libyaml-syck-perl +libmime-base64-perl libmime-lite-perl libstring-mkpasswd-perl \ +libtemplate-perl libyaml-syck-perl diff --git a/fripost-adduser b/fripost-adduser index 73ff70b..ba97cdf 100755 --- a/fripost-adduser +++ b/fripost-adduser @@ -20,34 +20,14 @@ use File::Slurp qw(slurp); use Fripost::Password; use Fripost::Prompt; use Fripost::Schema; -use IO::Prompt; use Getopt::Long; +use IO::Prompt; +use MIME::Base64; use MIME::Lite; +use MIME::QuotedPrint; +use Template; use YAML::Syck; -# Prompt for user info -sub read_user_info { - my $username = prompt_username("New username: "); - my $name = prompt "Full (real) name: "; - my $domain = (split /\@/, $username)[1]; - my $maildir = "$domain/". (split /\@/, $username)[0] . "/Maildir/"; # trailing slash important - my $active = 1; - my $password = prompt_password(); - - # Show the information that will be inserted - my $user = { - username => $username, - name => $name, - domain => $domain, - maildir => $maildir, - active => $active, - password => $password, - }; - print Dumper $user; - - return $user; -} - ## Get command line options our $conf = LoadFile('default.yml'); @@ -55,23 +35,42 @@ GetOptions( 'dbi_dsn' => \$conf->{dbi_dsn}, 'admuser=s' => \$conf->{admuser}, 'admpass=s' => \$conf->{admpass}, + 'debug' => \$conf->{debug}, 'pretend' => \$conf->{pretend}, + 'verbose' => \$conf->{verbose}, ) or die "Unable to get command line options."; +sub dsay { say @_ if $conf->{debug}; } +sub vsay { say @_ if $conf->{verbose} || $conf->{debug}; } + # Connect to the database my $schema = Fripost::Schema->connect( $conf->{dbi_dsn}, $conf->{admuser}, $conf->{admpass}, {} #\%dbi_params ); -say "Adding a new virtual user."; +my $user; +{ + my $username = prompt_email("New username: ", 'is_user'); + my $name = prompt "Full (real) name: "; + my $domain = (split /\@/, $username)[1]; + my $maildir = "$domain/". (split /\@/, $username)[0] . "/Maildir/"; # trailing slash important + my $active = 1; + my $password = prompt_password(); -my $user = read_user_info(); + $user = { + username => $username, + name => $name, + domain => $domain, + maildir => $maildir, + active => $active, + password => $password, + }; -ask_if_ok_or_abort(); + say "User name: $user->{username}"; + say "Real name: $user->{name}"; + say "Password: (hidden)"; -if ($conf->{pretend}) { - say "Nothing to do since we are pretending..."; - exit 0; + confirm_or_abort(); } die "User already exists" @@ -79,21 +78,81 @@ die "User already exists" username => $user->{username} })->count); ## Insert user into database -my $db_user = $schema->resultset('Mailbox')->new($user); -$db_user->insert; -say "New account $user->{username} added."; +if ($conf->{pretend}) { + vsay "Did not create user since we are pretending." +} +else { + $schema->resultset('Mailbox')->new($user)->insert; + say "New account $user->{username} added."; +} + +### Prepare sending emails + +my $tt = Template->new({ + INCLUDE_PATH => "$Bin/templ", + INTERPOLATE => 1, +}) || die "$Template::ERROR\n"; -## Send email my $msg = MIME::Lite->new( - From => 'admin@fripost.org', - To => $user->{username}, - Subject => 'Välkommen till Fripost!', - Data => scalar slurp('templ/new_user_mail.tt'), # TODO: actually use TT + From => 'Friposts administratörer <admin@fripost.org>', + Subject => "=?UTF-8?B?" . encode_base64('Välkommen till Fripost!' . "?=", + Encoding => 'quoted-printable', ); -$msg->send(); -say "Sent welcome message: "; -say $msg->as_string; +$msg->attr('content-type.charset' => 'utf-8'); + +### Send welcome email to new user +{ + my ($vars, $data); + $vars = {}; + $tt->process('new_user_mail.tt', $vars, \$data) + || die $tt->error(), '\n'; + + $msg->replace(To => $user->{username}); + $msg->data($data); + + + unless ($conf->{pretend}) { + $msg->send() unless $conf->{pretend};; + say "Sent welcome message."; + } + dsay "-----------------------------------"; + dsay "| Welcome mail |"; + dsay "-----------------------------------"; + dsay decode_qp($msg->as_string); + dsay "-----------------------------------"; +} + +### Send login credentials to new user +{ + my ($vars, $data); + $vars = { + user => $user->{username}, + pass => $user->{password}, + real => $user->{name}, + }; + $tt->process('user_info.tt', $vars, \$data) + || die $tt->error(), '\n'; + + $msg->data($data); + + dsay "-----------------------------------"; + dsay "| Login credentials mail |"; + dsay "-----------------------------------"; + dsay decode_qp($msg->as_string); + dsay "-----------------------------------"; + + confirm_or_abort("Send email with login information? "); + my $to = prompt_email("Where should the email be sent? "); + $msg->replace(To => $to); + if (!$conf->{pretend}) { + $msg->send; + say "Credentials sent."; + } + else { + say "Pretending, will not send credentials."; + } +} =head1 AUTHOR diff --git a/fripost-newalias b/fripost-newalias index 162f787..fb9acec 100755 --- a/fripost-newalias +++ b/fripost-newalias @@ -70,8 +70,8 @@ if (@addr == 0) { say "No valid destination adresses. Aborting..."; exit 1; } -say "dest adress: " . (join " ", @addr); -ask_if_ok_or_abort(); +say "from adress: " . (join " ", @addr); +confirm_or_abort(); ## Insert alias into database for my $addr (@addr) { diff --git a/fripost-passwd b/fripost-passwd index ad835b0..c01ca4b 100755 --- a/fripost-passwd +++ b/fripost-passwd @@ -13,22 +13,12 @@ fripost-passwd - Change password of user use FindBin qw($Bin); use lib "$Bin/lib"; -our $VERSION = '0.01'; - use Fripost::Password; use Fripost::Prompt; use Fripost::Schema; use Getopt::Long; use YAML::Syck; -my $username = $ARGV[0]; -$username //= prompt_username(); -my $password = prompt_password(); - -# Show the information that will be inserted -say "Password: $password"; -say "Salted MD5: " . smd5($password); - ## Get command line options our $conf = LoadFile('default.yml'); @@ -39,6 +29,10 @@ GetOptions( 'pretend' => \$conf->{pretend}, ) or die "Unable to get command line options."; +my $username = $ARGV[0]; +$username //= prompt_email("New username: ", 'is_user'); +my $password = prompt_password(); + if ($conf->{pretend}) { say "Nothing to do since we are pretending..."; exit 0; @@ -48,17 +42,12 @@ if ($conf->{pretend}) { my $schema = Fripost::Schema->connect( $conf->{dbi_dsn}, $conf->{admuser}, $conf->{admpass}, {} #\%dbi_params ); - my $row = $schema->resultset('Mailbox')->find($username); - -$row->password(smd5($password)); - +$row->password($password); $row->update; say "Updated password for $username."; -# TODO: ändra changedate vid varje insert - =head1 AUTHOR Stefan Kangas C<< <skangas at skangas.se> >> diff --git a/lib/Fripost/Logger.pm b/lib/Fripost/Logger.pm index c515a5c..0aacf2c 100755 --- a/lib/Fripost/Logger.pm +++ b/lib/Fripost/Logger.pm @@ -1,7 +1,6 @@ -#!/usr/bin/perl +package Fripost::Logger; use 5.010_000; -use warnings; use strict; =head1 NAME diff --git a/lib/Fripost/Password.pm b/lib/Fripost/Password.pm index 767bee1..038d835 100755 --- a/lib/Fripost/Password.pm +++ b/lib/Fripost/Password.pm @@ -1,7 +1,6 @@ -#!/usr/bin/perl +package Fripost::Password; use 5.010_000; -use warnings; use strict; =head1 NAME @@ -18,6 +17,7 @@ use Exporter; use MIME::Base64; our @EXPORT = qw/smd5 make_salt/; +our @ISA = qw(Exporter); sub smd5 { my $pw = shift; diff --git a/lib/Fripost/Prompt.pm b/lib/Fripost/Prompt.pm index 514a0b7..b41f806 100755 --- a/lib/Fripost/Prompt.pm +++ b/lib/Fripost/Prompt.pm @@ -1,7 +1,6 @@ -#!/usr/bin/perl +package Fripost::Prompt; use 5.010_000; -use warnings; use strict; =head1 NAME @@ -18,26 +17,57 @@ use Exporter; use IO::Prompt; use String::MkPasswd qw/mkpasswd/; -our @EXPORT = qw(prompt_password prompt_username); +our @EXPORT = qw(confirm_or_abort fix_username prompt_email prompt_password); +our @ISA = qw(Exporter); + +sub confirm_or_abort { + my ($msg) = @_; + $msg //= "Is this OK? [no will abort] "; + my $confirmed = prompt $msg, -ynt; + unless ($confirmed) { + say "User aborted"; + exit 1; + } +} sub fix_username { my ($nam) = @_; if ($nam !~ /\@/) { $nam .= '@fripost.org'; - say "Using $nam"; + say "Using username: $nam"; } return $nam; } +sub prompt_email { + my ($msg, $is_username) = @_; + $msg //= "Enter email: "; + my $email; + while (not defined $email) { + $email = prompt $msg; + + if ($is_username) { + $email = fix_username($email) + } + + if (!Email::Valid->address($email)) { + undef $email; + say "This is not a valid e-mail address. Try again." + } + } + return $email; + +} + sub prompt_password { - my ($prompt, $prompt2) = @_; - $prompt //= "Enter new password (blank for random): "; - $prompt2 //= "Enter new password again (blank for random): "; + my ($msg, $msg2) = @_; + $msg //= "Enter new password (blank for random): "; + $msg2 //= "Enter new password again (blank for random): "; my $password; while (not defined $password) { - $password = prompt $prompt, -e => '*'; - my $confirm = prompt $prompt2, -e => '*'; + $password = prompt $msg, -e => '*'; + my $confirm = prompt $msg2, -e => '*'; unless ($password eq $confirm) { undef $password; say "Passwords do not match"; @@ -50,32 +80,9 @@ sub prompt_password { -minnum => 2, -minspecial => 2, ); - say "Generated password: $password"; - } - return smd5($password); -} - -sub prompt_username { - my $prompt = shift; - $prompt //= "Enter username: "; - my $nam; - while (not defined $nam) { - $nam = prompt $prompt; - $nam = fix_username($nam); - if (!Email::Valid->address($nam)) { - undef $nam; - say "This is not a valid e-mail address. Try again." - } - } - return $nam; -} - -sub ask_if_ok_or_abort { - my $confirmed = prompt "Is this OK? [no will abort]", -ynt; - unless ($confirmed) { - say "User aborted"; - exit 1; + say "Using password: $password"; } + return $password; } =head1 AUTHOR diff --git a/lib/Fripost/Schema.pm b/lib/Fripost/Schema.pm index 440d2ed..c9cc832 100755 --- a/lib/Fripost/Schema.pm +++ b/lib/Fripost/Schema.pm @@ -1,7 +1,6 @@ package Fripost::Schema; use 5.010_000; -use warnings; use strict; use base qw/DBIx::Class::Schema/; diff --git a/lib/Fripost/Schema/Result/Mailbox.pm b/lib/Fripost/Schema/Result/Mailbox.pm index f12e1f7..48d81fb 100644 --- a/lib/Fripost/Schema/Result/Mailbox.pm +++ b/lib/Fripost/Schema/Result/Mailbox.pm @@ -6,6 +6,8 @@ use strict; use base qw/DBIx::Class::Core/; +use Fripost::Password; + # mysql> describe mailbox; # +-------------+--------------+------+-----+---------------------+-------+ # | Field | Type | Null | Key | Default | Extra | @@ -32,6 +34,22 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key('username'); +=head2 store_column + +override store_column to encrypt the password when stored + +=cut + +sub store_column { + my ($self, $col, $val) = @_; + + if ($col eq 'password') { + $val = smd5($val); + } + + return $self->next::method($col,$val); +} + =head1 NAME Fripost::Schema::Result::Mailbox - diff --git a/templ/new_user_mail.tt b/templ/new_user_mail.tt index ba72c57..82eeac6 100644 --- a/templ/new_user_mail.tt +++ b/templ/new_user_mail.tt @@ -4,11 +4,11 @@ Allmänna frågor kring programmen, webmailen eller konfiguration av e-postprogram tas bäst på e-postlistan så att svaren kan komma alla till del. -Du kan bli medlem genom att skicka ett mail till +Du kan bli medlem på e-postlistan genom att skicka ett mail till: members-subscribe@lists.fripost.org -Frågor gällande specifikt ditt konto kan du ta direkt med administratörerna. +Frågor gällande specifikt ditt konto kan du ta direkt med administratörerna: admin@fripost.org diff --git a/templ/user_info.tt b/templ/user_info.tt new file mode 100644 index 0000000..e6b778f --- /dev/null +++ b/templ/user_info.tt @@ -0,0 +1,28 @@ +Hej [% real %], + +Du är nu tillagd på Friposts system. + +Användarnamn: [% user %] +Lösenord [% pass %] + +Tänk på att vara försiktig med dina uppgifter. Spara en kopia av det här mailet +på en säker plats. + +Du kan logga in på: + + https://mail.fripost.org/ + +Frågor gällande ditt konto kan du ta direkt med administratörerna. + + admin@fripost.org + +Du kan hitta information om hur du konfigurerar din e-postklient för Fripost på +vår wiki. Vi försöker bygga upp medlemswikin till att bli den bästa resursen +för intern information kring föreningen. Du får gärna hjälpa till! + + http://wiki.fripost.org/ + +Ha kul! + +Med vänliga hälsningar, +Administratörerna |