Files
spook/new-user/nybruker.pl
2026-04-13 01:15:07 +09:00

211 lines
5.0 KiB
Perl
Executable File

#! /usr/bin/perl
use strict;
use warnings;
use Term::ReadLine; # Sørg for at libterm-readline-gnu-perl er installert!
use FindBin;
##
## KONSTANTER
##
my $batchmode = 0; # Hvis true, ikke still spørsmål om autodetekterte variabler
my $homepath = "/home/pvv/d";
my $gitrepos = "root\@localhost:/local/adm/git/pillar.git";
my $mailtemplate = $FindBin::Bin . "/velkommenmail.template";
my $gitdir = $ENV{HOME}."/.nybruker-git";
my $passwd = "$gitdir/files/passwd/passwd.pvv";
##
## FELLES VARIABLER
##
our $term = new Term::ReadLine 'PVV Input';
##
## HOVEDPROGRAM
##
foreach my $option (@ARGV) {
$batchmode = 1 if $option eq "-b";
}
# Clone the repository
if (-e $gitdir) {
vsystem("rm", "-rf", $gitdir);
}
vsystem("git", "clone", $gitrepos, $gitdir);
# Det er viktig at passwrdfilen finnes
die "$passwd not found" unless -e $passwd;
my %ui = &getuserinfo($passwd);
# Sjekk at uid/gid er definert
&checkuserdata(%ui);
&createprincipal(%ui);
&makeuser($passwd, "$ui{name}:*K*:$ui{uid}:$ui{gid}:$ui{gecos}:" .
"$ui{dir}:$ui{shell}");
&sendmail($gitdir, $mailtemplate, %ui);
chdir($gitdir);
vsystem("git", "commit", "-a", "-m", "Adding user $ui{gecos} <$ui{name}\@pvv.ntnu.no>");
vsystem("git", "push");
print <<EOF;
Bruker er opprettet og lagt til i salt.
Husk å også legge brukeren til i mdboh:
ssh postgres.pvv.ntnu.no
su -
su - pvv
mdboh ny $ui{name} "$ui{gecos}"
Når du er ferdig med å opprette brukere, logg inn på et par maskiner og kjør salt:
salt-call state.highstate
EOF
exit(0);
##
## SUBRUTINER
##
sub sendmail {
my ($gitdir, $mailtemplate, %ui) = @_;
my $tmpfile = "/tmp/nybruker.$$";
&vsystem("m4 -DUSERNAME=$ui{name} " .
"-DEMAIL=$ui{email} " .
"$mailtemplate > $tmpfile");
my $editor = &ask("Editor (brukes kun for aa redigere epost som sendes)", $ENV{EDITOR} || "vim") unless $batchmode;
vsystem($editor, $tmpfile) unless $batchmode;
my $confirm = "yes";
$confirm = &ask("Send?", $confirm) unless $batchmode;
return unless ($confirm =~ m/^[yY]/);
vsystem("/usr/sbin/sendmail " . $ui{email} . " < $tmpfile");
vsystem("rm", $tmpfile);
}
sub vsystem {
my $rc;
do {
print(join(" ", map { $a = /\s/ ? "'$_'" : $_ } @_), "\n");
system(@_);
$rc = $?;
if ($rc) {
$rc = &ask("Systemkall feilet, prøv igjen?", "ja") !~ /n/i;
}
} while ($rc); # Repeat until successful
}
sub makeuser {
my ($passwd, $pwline) = @_;
open (my $passwd_fd, ">>", $passwd) or die "Unable to open $passwd: $!";
print $passwd_fd "$pwline\n";
close $passwd_fd;
}
sub getuserinfo {
my ($passwd) = @_;
my %ui;
for(my $i = 0; $i < $#ARGV; $i++) {
if ($ARGV[$i] eq "-n") {
$ui{name} = $ARGV[$i + 1];
$i++;
} elsif ($ARGV[$i] eq "-u") {
$ui{uid} = $ARGV[$i + 1];
$i++;
} elsif ($ARGV[$i] eq "-g") {
$ui{gid} = $ARGV[$i + 1];
$i++;
} elsif ($ARGV[$i] eq "-d") {
$ui{dir} = $ARGV[$i + 1];
$i++;
} elsif ($ARGV[$i] eq "-s") {
$ui{shell} = $ARGV[$i + 1];
$i++;
} elsif ($ARGV[$i] eq "-e") {
$ui{email} = $ARGV[$i + 1];
$i++;
}
}
my %users;
my %uids;
open (my $passwd_fd, $passwd) or die "Unable to open $passwd: $!";
while (<$passwd_fd>) {
chomp;
my ($user, undef, $uid, $gid) = split(":");
$users{$user} = $_;
$uids{$uid} = $_;
}
$ui{name} = &ask("User name", $ui{name}) unless ($ui{name} && $batchmode);
die "Brukernavn $ui{name} finnes allerede!\n" if exists $users{$ui{name}};
# my $pwent = `grep '^$ui{name}\:' /local/pwdist/passwd`;
my $pwent_str = `/usr/bin/python3 $ENV{'HOME'}/salt/standard/passwd/ask_stud_ldap.py $ui{name}`;
chomp($pwent_str);
my @pwent = split(":", $pwent_str);
if (scalar @pwent >= 5) {
$ui{uid} = $pwent[2] unless $ui{uid};
$ui{gid} = $pwent[3] unless $ui{gid};
$ui{gecos} = $pwent[4] unless $ui{gecos};
} else {
# Bruker finnes ikke i passordfilen, og er altså litt sær.
# Da er det nok best vi spør om alt.
$batchmode = 0;
}
$ui{uid} = &ask("UID", $ui{uid}) unless ($ui{uid} && $batchmode);
die "UID $ui{uid} finnes allerede!" if exists $uids{$ui{uid}};
$ui{gid} = &ask("GID, should be 13401", $ui{gid}) unless ($ui{gid} && $batchmode);
$ui{gecos} = &ask("Full name", $ui{gecos}) unless ($ui{gecos} && $batchmode);
$ui{dir} = $homepath . "/" . $ui{name} unless $ui{dir};
$ui{dir} = &ask("Home, should be /home/pvv/d/$ui{name}", $ui{dir}) unless ($batchmode);
$ui{email} = $ui{name} . '@stud.ntnu.no' unless $ui{email};
$ui{email} = &ask("E-mail", $ui{email}) unless ($batchmode);
$ui{shell} = "/bin/bash" unless $ui{shell};
$ui{shell} = &ask("Shell, should be /bin/bash", $ui{shell}) unless ($ui{shell} && $batchmode);
return %ui;
}
sub ask {
my ($prompt, $default) = @_;
return $term->readline($prompt . ": ", $default);
}
sub checkuserdata {
my (%ui) = @_;
if ($ui{uid} eq "") {
die "UID er ikkje definert\n";
}
if ($ui{gid} eq "") {
die "GID er ikkje definert\n";
}
}
sub createprincipal {
my %ui = @_;
my $adminprincipal = &ask( 'Admin principal' , $ENV{LOGNAME} . "/admin" ) unless $batchmode;
if ($adminprincipal ne '' ){
vsystem("kadmin -p $adminprincipal add $ui{name}");
}
return;
}