#!/usr/bin/perl
#
# Copyright (C) 2014 Nethesis S.r.l.
# http://www.nethesis.it - support@nethesis.it
#
# This script is part of NethServer.
#
# NethServer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# NethServer 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. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NethServer. If not, see .
#
use strict;
use esmith::event;
use File::Temp qw(tempfile);
use Sys::Hostname;
my $file = shift;
my $separator = shift;
my ($systemName, $domainName) = split(/\./, Sys::Hostname::hostname(), 2);
if($file) {
open(FH, "<", $file) or die;
} else {
open(FH, "-");
}
if( ! $separator) {
$separator = "\t";
}
while() {
# Remove trailing whitespace:
chomp $_;
$_ =~ s/\s+$//;
my ($username, $fullName, $password) = split(/$separator/, $_);
if( ! $username) {
next;
}
if( ! $fullName) {
warn "[WARNING] Account `$username` is missing full name column: skipped.\n";
next;
}
if( ! esmith::event::event_signal('user-create', $username, $fullName) ) {
warn "[ERROR] Account `$username` user-create event failed.\n";
next;
}
system('/usr/sbin/sss_cache', '-u', $username);
system('/usr/sbin/sss_cache', '-u', "$username\@$domainName");
if($password) {
my ($pfh, $pfilename) = tempfile('import_users_XXXXX', UNLINK=>0, DIR=>'/tmp');
print $pfh $password;
close($pfh);
if( ! esmith::event::event_signal('password-modify', "$username\@$domainName", $pfilename) ) {
warn "[ERROR] Account `$username` user-create event failed.\n";
next;
}
unlink $pfilename;
}
warn "[INFO] imported $username as $username\@$domainName\n";
}