diff options
author | Stefan Kangas <stefankangas@gmail.com> | 2010-12-31 00:09:41 +0100 |
---|---|---|
committer | Stefan Kangas <stefankangas@gmail.com> | 2010-12-31 00:09:41 +0100 |
commit | 999461003a84603f188d69a2281c0aa6071fafeb (patch) | |
tree | 3deeb5f80208e8171591ced9fcd4ef5f7155de24 | |
parent | 7a718391b4f8ec8e85902abb55f3511db7eecb59 (diff) |
Automatic password generation for new users
-rwxr-xr-x | fripost-adduser.pl | 41 | ||||
-rwxr-xr-x | fripost-mkpass.pl | 43 | ||||
-rwxr-xr-x | lib/Fripost/Password.pm | 66 |
3 files changed, 139 insertions, 11 deletions
diff --git a/fripost-adduser.pl b/fripost-adduser.pl index d6ebe53..b0f107e 100755 --- a/fripost-adduser.pl +++ b/fripost-adduser.pl @@ -25,9 +25,11 @@ our $VERSION = '0.01'; use Data::Dumper; use DateTime; use Email::Valid; +use Fripost::Password; use Fripost::Schema; use IO::Prompt; use Getopt::Long; +use String::MkPasswd qw/mkpasswd/; use YAML::Syck; ## Get command line options @@ -46,12 +48,14 @@ my $schema = Fripost::Schema->connect( 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 "Enter the full e-mail: "; + $user{username} = prompt "New username: "; if (!Email::Valid->address($user{username})) { undef $user{username}; - say "This is not a vaild e-mail address. Try again." + say "This is not a valid e-mail address. Try again." } } @@ -63,10 +67,6 @@ my @parts = split /\@/, $user{username}; my $username = $parts[0]; my $domain = $parts[1]; -print "Username: $username\nDomain:$domain\n"; - -die; - # Set domain name $user{domain} = $domain; @@ -80,10 +80,29 @@ $user{change_date} = $now; $user{active} = 1; -my $user = $schema->resultset('Mailbox')->new(\%user); - -# Make the insert into database -$user->insert; +# 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." +} =head1 AUTHOR @@ -91,7 +110,7 @@ Stefan Kangas C<< <skangas at skangas.se> >> =head1 COPYRIGHT -Copyright 2010 Stefan Kangas, all rights reserved. +Copyright 2010 Stefan Kangas. =head1 LICENSE diff --git a/fripost-mkpass.pl b/fripost-mkpass.pl new file mode 100755 index 0000000..4740f5b --- /dev/null +++ b/fripost-mkpass.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use 5.010_000; +use warnings; +use strict; + +=head1 NAME + +fripost-mkpass.pl - Create a random new password + +=cut + +our $VERSION = '0.01'; + +use Fripost::Password; +use String::MkPasswd qw/mkpasswd/; + +# Generate password +my $password = mkpasswd( + -length => 20, + -minnum => 5, + -minspecial => 3 +); +$user{password} = smd5($password); + +# Show the information that will be inserted +say "Generated password: $password"; +say "Salted MD5: " . smd5($password); + +=head1 AUTHOR + +Stefan Kangas C<< <skangas at skangas.se> >> + +=head1 COPYRIGHT + +Copyright 2010 Stefan Kangas. + +=head1 LICENSE + +This program is free software; you can redistribute it and/or modify it +under the same terms as perl itself. + +=cut diff --git a/lib/Fripost/Password.pm b/lib/Fripost/Password.pm new file mode 100755 index 0000000..eb5ad7e --- /dev/null +++ b/lib/Fripost/Password.pm @@ -0,0 +1,66 @@ +#!/usr/bin/perl + +use 5.010_000; +use warnings; +use strict; + +=head1 NAME + +Password.pm - Generate passwords + +=cut + +our $VERSION = '0.01'; + +use Data::Dumper; +use Digest::MD5; +use Exporter; +use MIME::Base64; + +our @EXPORT = qw/smd5 make_salt/ + +sub smd5 { + my $pw = shift; + my $salt = shift || &make_salt(); + return "{SMD5}" . pad_base64( MIME::Base64::encode( Digest::MD5::md5( $pw . $salt ) . $salt, '' ) ); +} + + +sub make_salt { + my $len = 8 + int( rand(8) ); + my @bytes = (); + for my $i ( 1 .. $len ) { + push( @bytes, rand(255) ); + } + return pack( 'C*', @bytes ); +} + +=head1 AUTHOR + +Stefan Kangas C<< <skangas at skangas.se> >> + +=head1 BUGS + +Please report any bugs to C<< <skangas at skangas.se> >> + +=head1 COPYRIGHT + +Copyright (c) 2010 Dominik Schulz (dominik.schulz@gauner.org). All rights reserved. + +Copyright 2010 Stefan Kangas, all rights reserved. + +=head1 LICENSE + +This program is free software; you can redistribute it and/or modify it under +the same terms as Perl itself. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +=cut + +1; # End of Password.pm + +__END__ + |