2003-05-13 10:54:53 +00:00
|
|
|
#! /usr/bin/perl
|
2003-05-07 16:53:03 +00:00
|
|
|
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# This code is part of LDAP Account Manager (http://www.sourceforge.net/projects/lam)
|
|
|
|
# Copyright (C) 2003 Tilo Lutz
|
|
|
|
#
|
|
|
|
# This program 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 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# LDAP Account Manager daemon to create and delete homedirecotries and quotas
|
2003-05-13 10:54:53 +00:00
|
|
|
|
2003-05-07 16:53:03 +00:00
|
|
|
|
2003-05-13 10:54:53 +00:00
|
|
|
#use strict; # Use strict for security reasons
|
2003-05-07 16:53:03 +00:00
|
|
|
|
2003-05-15 20:59:26 +00:00
|
|
|
@quota_grp;
|
2003-05-07 16:53:03 +00:00
|
|
|
@quota_usr; # Filesystems with enabled userquotas
|
2003-05-13 10:54:53 +00:00
|
|
|
# vals = DN, PAssword, user, home, (add|rem),
|
|
|
|
# quota, (set|get),(u|g), (mountpoint,blocksoft,blockhard,filesoft,filehard)+
|
|
|
|
# chown options
|
|
|
|
$|=1; # Disable buffering
|
2003-05-07 16:53:03 +00:00
|
|
|
|
|
|
|
sub get_fs { # Load mountpoints from mtab if enabled quotas
|
|
|
|
Quota::setmntent();
|
|
|
|
my $i=0;
|
|
|
|
my @args;
|
|
|
|
while (my @temp = Quota::getmntent()) {
|
|
|
|
$args[$i][0] = $temp[0];
|
|
|
|
$args[$i][1] = $temp[1];
|
|
|
|
$args[$i][2] = $temp[2];
|
|
|
|
$args[$i][3] = $temp[3];
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
Quota::endmntent();
|
|
|
|
my $j=0; my $k=0; $i=0;
|
|
|
|
while ($args[$i][0]) {
|
|
|
|
if ( $args[$i][3] =~ m/usrquota/ ) {
|
|
|
|
$quota_usr[$j][0] = $args[$i][0];
|
|
|
|
$quota_usr[$j][1] = $args[$i][1];
|
|
|
|
$quota_usr[$j][2] = $args[$i][2];
|
|
|
|
$quota_usr[$j][3] = $args[$i][3];
|
|
|
|
$j++;
|
|
|
|
}
|
2003-09-19 10:03:00 +00:00
|
|
|
if ( $args[$i][3] =~ m/grpquota/ ) {
|
2003-05-07 16:53:03 +00:00
|
|
|
$quota_grp[$k][0] = $args[$i][0];
|
|
|
|
$quota_grp[$k][1] = $args[$i][1];
|
|
|
|
$quota_grp[$k][2] = $args[$i][2];
|
|
|
|
$quota_grp[$k][3] = $args[$i][3];
|
|
|
|
$k++;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-13 10:54:53 +00:00
|
|
|
# ***************** Check values
|
2006-02-27 13:22:00 +00:00
|
|
|
if ($< == 0 ) { # we are root
|
2003-09-20 13:01:09 +00:00
|
|
|
# Drop root Previleges
|
|
|
|
($<, $>) = ($>, $<);
|
2003-11-06 10:58:21 +00:00
|
|
|
if ($ARGV[0] eq "*test") {
|
|
|
|
use Quota; # Needed to get and set quotas
|
|
|
|
print "Perl quota module successfully installed.\n";
|
2004-02-07 11:55:40 +00:00
|
|
|
print "If you haven't seen any errors lamdaemon.pl was set up successfully.\n";
|
2003-11-06 10:58:21 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
# loop for every transmitted user
|
2003-11-18 08:06:46 +00:00
|
|
|
my $string = do {local $/;<STDIN>};
|
|
|
|
@input = split ("\n", $string );
|
|
|
|
for ($i=0; $i<=$#input; $i++) {
|
2003-11-06 10:58:21 +00:00
|
|
|
$return = "";
|
2003-11-18 08:06:46 +00:00
|
|
|
@vals = split (' ', $input[$i]);
|
2003-11-06 10:58:21 +00:00
|
|
|
switch: {
|
|
|
|
# Get user information
|
|
|
|
if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); }
|
|
|
|
else { @user = getgrnam($vals[0]); }
|
|
|
|
$vals[1] eq 'home' && do {
|
|
|
|
switch2: {
|
|
|
|
$vals[2] eq 'add' && do {
|
2006-02-27 13:22:00 +00:00
|
|
|
# split homedir to set all directories below the last dir. to 0755
|
2003-11-06 10:58:21 +00:00
|
|
|
my $path = $user[7];
|
|
|
|
$path =~ s,/(?:[^/]*)$,,;
|
|
|
|
($<, $>) = ($>, $<); # Get root privileges
|
|
|
|
if (! -e $path) {
|
2006-02-27 13:22:00 +00:00
|
|
|
system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir
|
2003-11-06 10:58:21 +00:00
|
|
|
}
|
|
|
|
if (! -e $user[7]) {
|
2006-02-27 13:22:00 +00:00
|
|
|
system 'mkdir', '-m', '0755', $user[7]; # Create homedir itself
|
|
|
|
system ("(cd /etc/skel && tar cf - .) | (cd $user[7] && tar xf -)"); # Copy /etc/sekl into homedir
|
|
|
|
system 'chown', '-hR', "$user[2]:$user[3]" , $user[7]; # Change owner to new user
|
2003-11-06 10:58:21 +00:00
|
|
|
if (-e '/usr/sbin/useradd.local') {
|
|
|
|
system '/usr/sbin/useradd.local', $user[0]; # run useradd-script
|
|
|
|
}
|
|
|
|
}
|
2004-02-07 11:55:40 +00:00
|
|
|
else {
|
|
|
|
$return = "ERROR,Lamdaemon,Homedirectory already exists.:$return";
|
|
|
|
}
|
2003-11-06 10:58:21 +00:00
|
|
|
($<, $>) = ($>, $<); # Give up root previleges
|
|
|
|
last switch2;
|
|
|
|
};
|
|
|
|
$vals[2] eq 'rem' && do {
|
|
|
|
($<, $>) = ($>, $<); # Get root previliges
|
2006-02-27 13:22:00 +00:00
|
|
|
if (-d $user[7] && $user[7] ne '/') {
|
2004-02-07 11:55:40 +00:00
|
|
|
# Fixme, only delete files owned by user.
|
2003-11-06 10:58:21 +00:00
|
|
|
system 'rm', '-R', $user[7]; # Delete Homedirectory
|
|
|
|
if (-e '/usr/sbin/userdel.local') {
|
|
|
|
system '/usr/sbin/userdel.local', $user[0];
|
|
|
|
}
|
|
|
|
}
|
2004-02-07 11:55:40 +00:00
|
|
|
else {
|
|
|
|
$return = "ERROR,Lamdaemon,Homedirectory doesn't exists.:$return";
|
|
|
|
}
|
2003-11-06 10:58:21 +00:00
|
|
|
($<, $>) = ($>, $<); # Give up root previleges
|
|
|
|
last switch2;
|
|
|
|
};
|
2003-09-20 13:01:09 +00:00
|
|
|
}
|
2004-02-07 11:55:40 +00:00
|
|
|
# Show error if undfined command is used
|
|
|
|
$return = "ERROR,Lamdaemon,Unknown command $vals[2].:$return";
|
2003-11-06 10:58:21 +00:00
|
|
|
last switch;
|
2003-09-20 13:01:09 +00:00
|
|
|
};
|
2003-11-06 10:58:21 +00:00
|
|
|
$vals[1] eq 'quota' && do {
|
|
|
|
use Quota; # Needed to get and set quotas
|
|
|
|
get_fs(); # Load list of devices with enabled quotas
|
|
|
|
# Store quota information in array
|
2003-11-06 14:53:00 +00:00
|
|
|
@quota_temp1 = split (':', $vals[4]);
|
2003-11-06 10:58:21 +00:00
|
|
|
$group=0;
|
2003-09-20 13:01:09 +00:00
|
|
|
$i=0;
|
2003-11-06 10:58:21 +00:00
|
|
|
while ($quota_temp1[$i]) {
|
|
|
|
$j=0;
|
|
|
|
@temp = split (',', $quota_temp1[$i]);
|
|
|
|
while ($temp[$j]) {
|
|
|
|
$quota[$i][$j] = $temp[$j];
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
$i++;
|
2003-11-06 14:53:00 +00:00
|
|
|
}
|
|
|
|
if ($vals[3] eq 'user') { $group=false; }
|
|
|
|
else {
|
|
|
|
$group=1;
|
|
|
|
@quota_usr = @quota_grp;
|
|
|
|
}
|
|
|
|
switch2: {
|
|
|
|
$vals[2] eq 'rem' && do {
|
|
|
|
$i=0;
|
|
|
|
($<, $>) = ($>, $<); # Get root privileges
|
|
|
|
while ($quota_usr[$i][0]) {
|
|
|
|
$dev = Quota::getqcarg($quota_usr[$i][1]);
|
|
|
|
$return = Quota::setqlim($dev,$user[2],0,0,0,0,1,$group);
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
($<, $>) = ($>, $<); # Give up root previleges
|
|
|
|
last switch2;
|
|
|
|
};
|
|
|
|
$vals[2] eq 'set' && do {
|
|
|
|
$i=0;
|
|
|
|
($<, $>) = ($>, $<); # Get root privileges
|
|
|
|
while ($quota_usr[$i][0]) {
|
|
|
|
$dev = Quota::getqcarg($quota[$i][0]);
|
|
|
|
$return = Quota::setqlim($dev,$user[2],$quota[$i][1],$quota[$i][2],$quota[$i][3],$quota[$i][4],1,$group);
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
($<, $>) = ($>, $<); # Give up root previleges
|
|
|
|
last switch2;
|
|
|
|
};
|
|
|
|
$vals[2] eq 'get' && do {
|
|
|
|
$i=0;
|
|
|
|
($<, $>) = ($>, $<); # Get root privileges
|
|
|
|
while ($quota_usr[$i][0]) {
|
2003-11-08 12:44:26 +00:00
|
|
|
if ($vals[0]ne'+') {
|
2003-11-06 10:58:21 +00:00
|
|
|
$dev = Quota::getqcarg($quota_usr[$i][1]);
|
2003-11-06 14:53:00 +00:00
|
|
|
@temp = Quota::query($dev,$user[2],$group);
|
|
|
|
if ($temp[0]ne'') {
|
|
|
|
$return = "$quota_usr[$i][1],$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$temp[7]:$return";
|
2003-11-06 10:58:21 +00:00
|
|
|
}
|
|
|
|
else { $return = "$quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
|
|
|
|
}
|
2003-11-06 14:53:00 +00:00
|
|
|
else { $return = "$quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
($<, $>) = ($>, $<); # Give up root previleges
|
|
|
|
last switch2;
|
|
|
|
};
|
2004-02-07 11:55:40 +00:00
|
|
|
$return = "ERROR,Lamdaemon,Unknown command $vals[2].:$return";
|
2003-11-06 14:53:00 +00:00
|
|
|
}
|
2003-09-20 13:01:09 +00:00
|
|
|
};
|
2003-11-06 10:58:21 +00:00
|
|
|
last switch;
|
2004-02-07 11:55:40 +00:00
|
|
|
$return = "ERROR,Lamdaemon,Unknown command $vals[1].:$return";
|
2003-11-06 10:58:21 +00:00
|
|
|
};
|
|
|
|
print "$return\n";
|
|
|
|
}
|
|
|
|
}
|
2003-05-07 16:53:03 +00:00
|
|
|
}
|
2003-08-11 21:09:17 +00:00
|
|
|
else {
|
2003-09-20 13:59:13 +00:00
|
|
|
$hostname = shift @ARGV;
|
|
|
|
$remotepath = shift @ARGV;
|
2003-09-20 13:01:09 +00:00
|
|
|
use Net::SSH::Perl;
|
2003-10-23 18:25:55 +00:00
|
|
|
if ($ARGV[2] eq "*test") { print "Net::SSH::Perl successfully installed.\n"; }
|
2005-03-05 13:23:59 +00:00
|
|
|
if (($ARGV[0] eq "-") and ($ARGV[1] eq "-")) { # user+passwd are in STDIN
|
|
|
|
$username = <STDIN>;
|
|
|
|
chop($username);
|
|
|
|
@username = split (',', $username);
|
|
|
|
$username[0] =~ s/uid=//;
|
|
|
|
$username[0] =~ s/cn=//;
|
|
|
|
$username = $username[0];
|
|
|
|
$password = <STDIN>;
|
|
|
|
chop($password);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
@username = split (',', $ARGV[0]);
|
|
|
|
$username[0] =~ s/uid=//;
|
|
|
|
$username[0] =~ s/cn=//;
|
|
|
|
$username = $username[0];
|
|
|
|
$password = $ARGV[1];
|
|
|
|
}
|
2003-11-06 10:58:21 +00:00
|
|
|
# Put all transfered lines in one string
|
|
|
|
if ($ARGV[2] ne "*test") {
|
2003-11-18 08:06:46 +00:00
|
|
|
$string = do {local $/;<STDIN>};
|
2003-11-06 10:58:21 +00:00
|
|
|
}
|
2005-10-03 10:41:16 +00:00
|
|
|
else {
|
|
|
|
$argv = "*test\n";
|
|
|
|
$string = " \n";
|
|
|
|
}
|
2003-09-20 13:59:13 +00:00
|
|
|
my $ssh = Net::SSH::Perl->new($hostname, options=>[
|
2003-11-15 11:37:15 +00:00
|
|
|
"UserKnownHostsFile /dev/null"],
|
2005-10-07 16:07:07 +00:00
|
|
|
protocol => "2,1", debug => 0 );
|
2005-03-05 13:23:59 +00:00
|
|
|
$ssh->login($username, $password);
|
2003-11-24 20:32:37 +00:00
|
|
|
# Change needed to prevent buffer overrun
|
|
|
|
@string2 = split ("\n", $string);
|
|
|
|
for ($i=0; $i<=$#string2; $i++) {
|
|
|
|
($stdout2, $stderr, $exit) = $ssh->cmd("sudo $remotepath $argv", $string2[$i]);
|
|
|
|
$stdout .= $stdout2;
|
|
|
|
}
|
|
|
|
#($stdout, $stderr, $exit) = $ssh->cmd("sudo $remotepath $argv", $string);
|
2003-11-18 08:06:46 +00:00
|
|
|
print $stdout;
|
2003-09-20 13:01:09 +00:00
|
|
|
}
|