2022-04-20 02:02:57 +02:00
|
|
|
#!/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 } ],
|
2022-04-20 02:10:59 +02:00
|
|
|
[ 'size|s=s', 'Assigned size for all users.', { required => 1 } ],
|
2022-04-20 02:02:57 +02:00
|
|
|
[ '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);
|
|
|
|
}
|