Adding initial script.
This commit is contained in:
parent
24db3ede8b
commit
656429a7a7
19
Build.PL
Normal file
19
Build.PL
Normal file
@ -0,0 +1,19 @@
|
||||
use Module::Build;
|
||||
|
||||
my $home = $ENV{HOME};
|
||||
|
||||
my $build = Module::Build->new(
|
||||
module_name => 'QuotaUsers',
|
||||
license => 'GPLv3',
|
||||
dist_author => 'Sergio Iglesias <sergiotarxz@posteo.net>',
|
||||
dist_abstract => 'Creates users limited in quota.',
|
||||
requires => {
|
||||
'Getopt::Long::Descriptive' => '0',
|
||||
'Const::Fast' => '0',
|
||||
'Text::CSV' => 0,
|
||||
'Crypt::URandom' => 0,
|
||||
},
|
||||
install_path => {
|
||||
}
|
||||
);
|
||||
$build->create_build_script;
|
69
bin/quotausers
Normal file
69
bin/quotausers
Normal file
@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
use v5.30.0;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Getopt::Long::Descriptive;
|
||||
use Const::Fast;
|
||||
|
||||
use Text::CSV qw/csv/;
|
||||
use Crypt::URandom;
|
||||
|
||||
const my @CHARACTERS => ( 'a' .. 'z', 'A' .. 'Z' );
|
||||
|
||||
my ( $opt, $usage ) = describe_options(
|
||||
'quotausers %o',
|
||||
[ 'csv|c=s', 'The csv with the emails and usernames', { required => 1 } ],
|
||||
[ 'size|c=s', 'Assigned size for all users.', { required => 1 } ],
|
||||
[ 'help|h=s', 'Show help', { shortcircuit => 1 } ],
|
||||
);
|
||||
|
||||
print( $usage->text ), exit if $opt->help;
|
||||
|
||||
my $size = $opt->size;
|
||||
my $users = csv( in => $opt->csv, headers => 'auto' );
|
||||
|
||||
for my $user (@$users) {
|
||||
my $username = $user->{username};
|
||||
my $email = $user->{email};
|
||||
my $passwd = get_passwd();
|
||||
my $errorno = system qw/sudo useradd -m/, $username, qw{-s /bin/bash};
|
||||
my $full_size = 1024**2 * $size;
|
||||
my $size_softlimit = 1024**2 * ( $size - 1 );
|
||||
if ($errorno) {
|
||||
die 'Cannot create user. Are you admin?';
|
||||
}
|
||||
execute_chpasswd( $username, $passwd );
|
||||
system qw/sudo setquota -u/, $username, $size_softlimit, $full_size, 0, 0,
|
||||
'/';
|
||||
system qw/sudo gpasswd -a/, $username, 'video';
|
||||
system qw/sudo gpasswd -a/, $username, 'plugdev';
|
||||
system qw/sudo gpasswd -a/, $username, 'storage';
|
||||
system qw/sudo gpasswd -a/, $username, 'audio';
|
||||
system qw/sudo -u/, $username,
|
||||
qw{flatpak --user remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo};
|
||||
say "$username <$email> -- $passwd";
|
||||
}
|
||||
|
||||
sub execute_chpasswd {
|
||||
my $username = shift;
|
||||
my $passwd = shift;
|
||||
open my $fh, '|-', qw/sudo chpasswd/ or die "Unable to change passwd.";
|
||||
say $fh "$username:$passwd";
|
||||
close $fh;
|
||||
}
|
||||
|
||||
sub get_passwd {
|
||||
my $passwd = '';
|
||||
for ( 0 .. 11 ) {
|
||||
$passwd .=
|
||||
$CHARACTERS[ get_safe_random_64_bits_number() % scalar @CHARACTERS ];
|
||||
}
|
||||
return $passwd;
|
||||
}
|
||||
|
||||
sub get_safe_random_64_bits_number {
|
||||
return unpack 'Q', Crypt::URandom::urandom(8);
|
||||
}
|
Loading…
Reference in New Issue
Block a user