LDAPAccountManager/lam/lib/lamdaemon.pl

386 lines
10 KiB
Perl
Raw Normal View History

#! /usr/bin/perl
# $Id$
#
2009-10-27 18:47:12 +00:00
# This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2006-03-03 17:30:35 +00:00
# Copyright (C) 2003 - 2006 Tilo Lutz
2011-02-12 15:30:22 +00:00
# Copyright (C) 2006 - 2011 Roland Gruber
#
# 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
2008-07-21 20:06:11 +00:00
use Sys::Syslog;
# Defines the protocol version of the lamdaemon script.
# This will only be changed when additional commands are added etc.
my $LAMDAEMON_PROTOCOL_VERSION = 1;
my $SPLIT_DELIMITER = "###x##y##x###";
2006-02-27 13:26:35 +00:00
# set a known path
my $path = "";
if (-d "/sbin") {
if ($path eq "") { $path = "/sbin"; }
else { $path = "$path:/sbin"; }
}
if (-d "/usr/sbin") {
if ($path eq "") { $path = "/usr/sbin"; }
else { $path = "$path:/usr/sbin"; }
}
if (-l "/bin") {
if ($path eq "") { $path = "/usr/bin"; }
else { $path = "$path:/usr/bin"; }
}
else {
if ($path eq "") { $path = "/bin:/usr/bin"; }
else { $path = "$path:/bin:/usr/bin"; }
}
if (-d "/opt/sbin") { $path = "$path:/opt/sbin"; }
if (-d "/opt/bin") { $path = "$path:/opt/bin"; }
$ENV{"PATH"} = $path;
2007-02-25 13:48:13 +00:00
# get hostname
$hostname = `hostname`;
chop($hostname);
#use strict; # Use strict for security reasons
@quota_grp;
@quota_usr; # Filesystems with enabled userquotas
# vals = DN, PAssword, user, home, (add|rem),
# quota, (set|get),(u|g), (mountpoint,blocksoft,blockhard,filesoft,filehard)+
# chown options
$|=1; # Disable buffering
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]) {
2011-02-11 18:52:52 +00:00
if ( $args[$i][3] =~ m/usr[j]?quota/ ) {
$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++;
}
2011-02-11 18:52:52 +00:00
if ( $args[$i][3] =~ m/grp[j]?quota/ ) {
$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++;
}
}
# ***************** Check values
2009-06-14 11:36:38 +00:00
# check if script runs as root
if ($< != 0 ) {
2009-12-12 16:06:01 +00:00
print "ERROR,Lamdaemon ($hostname),Not called as root (user id " . $< . ").\n";
logMessage(LOG_ERR, "Not called as root (user id " . $< . ").");
2009-06-14 11:36:38 +00:00
exit 1;
}
# Drop root privileges
($<, $>) = ($>, $<);
my $input = $ARGV[0];
$return = "";
@vals = split ($SPLIT_DELIMITER, $input);
# Get user information
if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); }
else { @user = getgrnam($vals[0]); }
if ($vals[1] eq '') {
# empty line, nothing to do
}
elsif (($vals[1] eq 'test')) {
# run tests
runTest();
}
elsif ($vals[1] eq 'home') {
manageHomedirs();
}
elsif ($vals[1] eq 'quota') {
manageQuotas();
}
else {
$return = "ERROR,Lamdaemon ($hostname),Unknown command $vals[1].";
logMessage(LOG_ERR, "Unknown command $vals[1].");
2006-08-27 14:57:50 +00:00
}
print "$return\n";
2008-07-21 20:06:11 +00:00
2009-06-14 12:17:48 +00:00
#
# Runs tests to check the environment
#
sub runTest {
# protocol version check
if ($vals[2] eq 'version') {
if ($vals[3] eq $LAMDAEMON_PROTOCOL_VERSION) {
$return = "INFO,Version check ok";
}
else {
$return = "ERROR,Version check failed. Please upgrade the lamdaemon script to the same version as your main LAM installation.";
}
}
2009-06-14 12:17:48 +00:00
# basic test
elsif ($vals[2] eq 'basic') {
2010-01-02 18:40:29 +00:00
$return = "INFO,Basic test ok";
2009-06-14 12:17:48 +00:00
}
# quota test
elsif ($vals[2] eq 'quota') {
require Quota;
2010-01-02 18:40:29 +00:00
$return = "INFO,Quota test ok";
}
# NSS LDAP
elsif ($vals[2] eq 'nss') {
$userName = $vals[3];
# check if the user exists in /etc/passwd
system("grep", "-q", "^" . $userName . ":", "/etc/passwd");
if ( $? == 0 ) {
$error = "User $userName is a local user (/etc/passwd) but should be LDAP only.";
$return = "ERROR,$error";
logMessage(LOG_ERR, $error);
}
else {
# check if home directory is readable
@user = getpwnam($userName);
if ($user[7] eq '') {
$return = "ERROR,Unable to determine home directory of user $userName. Please check that NSS LDAP is correctly configured.";
logMessage(LOG_ERR, "Unable to determine home directory of user $userName. Please check that NSS LDAP is correctly configured.");
}
else {
$return = "INFO,NSS test ok";
}
}
2009-06-14 12:17:48 +00:00
}
else {
$return = "ERROR,Unknown test: $vals[2]";
}
2009-06-14 12:17:48 +00:00
}
2009-06-14 14:18:26 +00:00
#
# Handles all homedir related commands
#
sub manageHomedirs {
if ($vals[2] eq 'add') {
createHomedir();
}
elsif ($vals[2] eq 'rem') {
removeHomedir();
}
else {
# Show error if undefined command is used
$return = "ERROR,Lamdaemon ($hostname),Unknown home command $vals[2].";
logMessage(LOG_ERR, "Unknown command $vals[2]");
}
}
2009-06-14 12:17:48 +00:00
#
# Creates the homedirectory of the user
#
sub createHomedir {
my $homedir = $vals[3];
2009-06-14 14:30:41 +00:00
if ($homedir eq '') {
$return = "ERROR,Lamdaemon ($hostname),No home directory specified.";
logMessage(LOG_ERR, "No home directory specified.");
2009-06-14 14:30:41 +00:00
return;
}
2009-06-14 12:17:48 +00:00
my $path = $homedir;
# split homedir to set all directories below the last dir. to 0755
$path =~ s,/(?:[^/]*)$,,;
($<, $>) = ($>, $<); # Get root privileges
if (! -e $path) {
system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir
}
if (! -e $homedir) {
system 'mkdir', '-m', $vals[4], $homedir; # Create homedir itself
2009-06-14 12:17:48 +00:00
system ("(cd /etc/skel && tar cf - .) | (cd $homedir && tar xmf -)"); # Copy /etc/sekl into homedir
system 'chown', '-hR', "$vals[5]:$vals[6]" , $homedir; # Change owner to new user
2009-06-14 12:17:48 +00:00
if (-e '/usr/sbin/useradd.local') {
system '/usr/sbin/useradd.local', $vals[0]; # run useradd-script
system 'chmod', '-R', $vals[4], $homedir; # Edit chmod rights
2009-06-14 12:17:48 +00:00
}
system 'chmod', $vals[4], $homedir; # Edit chmod rights
2009-06-14 12:17:48 +00:00
$return = "INFO,Lamdaemon ($hostname),Home directory created (" . $homedir . ").";
logMessage(LOG_INFO, "Home directory created (" . $homedir . ")");
}
else {
$return = "ERROR,Lamdaemon ($hostname),Home directory already exists (" . $homedir . ").";
2009-06-14 14:30:41 +00:00
logMessage(LOG_ERR, "Home directory already exists (" . $homedir . ")");
2009-06-14 12:17:48 +00:00
}
($<, $>) = ($>, $<); # Give up root previleges
}
#
# Removes the homedirectory of the user
#
sub removeHomedir {
if ($vals[3] eq '') {
2009-06-14 14:30:41 +00:00
$return = "ERROR,Lamdaemon ($hostname),Home directory path is empty.";
logMessage(LOG_ERR, "Home directory path is empty.");
return;
}
2009-06-14 12:17:48 +00:00
($<, $>) = ($>, $<); # Get root previliges
if (-d $vals[3] && $vals[3] ne '/') {
if ((stat($vals[3]))[4] eq $vals[4]) {
system 'rm', '-R', $vals[3]; # Delete Homedirectory
2009-06-14 12:17:48 +00:00
if (-e '/usr/sbin/userdel.local') {
system '/usr/sbin/userdel.local', $vals[0];
2009-06-14 12:17:48 +00:00
}
$return = "Ok";
logMessage(LOG_INFO, "Home directory removed (" . $vals[3] . ")");
2009-06-14 12:17:48 +00:00
}
else {
2010-12-16 17:55:01 +00:00
$return = "ERROR,Lamdaemon ($hostname),Home directory not owned by $vals[4].";
logMessage(LOG_ERR, "Home directory owned by wrong user (" . $vals[4] . ")");
2009-06-14 12:17:48 +00:00
}
}
else {
$return = "INFO,Lamdaemon ($hostname),The directory which should be deleted was not found (skipped).";
}
($<, $>) = ($>, $<); # Give up root previleges
}
2009-06-14 14:18:26 +00:00
#
# Handles all quota related commands
#
sub manageQuotas {
require Quota; # Needed to get and set quotas
get_fs(); # Load list of devices with enabled quotas
# Store quota information in array
@quota_temp1 = split (':', $vals[4]);
$group=0;
$i=0;
while ($quota_temp1[$i]) {
$j=0;
@temp = split (',', $quota_temp1[$i]);
while ($temp[$j]) {
$quota[$i][$j] = $temp[$j];
$j++;
}
$i++;
}
if ($vals[3] eq 'user') { $group=false; }
else {
$group=1;
@quota_usr = @quota_grp;
}
if ($vals[2] eq 'rem') {
remQuotas();
}
elsif ($vals[2] eq 'set') {
setQuotas();
}
elsif ($vals[2] eq 'get') {
getQuotas();
}
else {
$return = "ERROR,Lamdaemon ($hostname),Unknown quota command $vals[2].";
logMessage(LOG_ERR, "Unknown command $vals[2].");
}
}
2009-06-14 12:17:48 +00:00
#
# Removes the quotas of a user or group
#
sub remQuotas {
$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
}
#
# Sets the quota values
#
sub setQuotas {
$i=0;
($<, $>) = ($>, $<); # Get root privileges
while ($quota[$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);
if ($return == -1) {
$return = "ERROR,Lamdaemon ($hostname),Unable to set quota!";
logMessage(LOG_ERR, "Unable to set quota for $user[0].");
}
else {
logMessage(LOG_INFO, "Set quota for $user[0].");
}
$i++;
}
($<, $>) = ($>, $<); # Give up root previleges
}
#
# Reads the quota values
#
sub getQuotas {
$i=0;
($<, $>) = ($>, $<); # Get root privileges
while ($quota_usr[$i][0]) {
if ($vals[0]ne'+') {
$dev = Quota::getqcarg($quota_usr[$i][1]);
@temp = Quota::query($dev,$user[2],$group);
if ($temp[0]ne'') {
if ($temp == -1) {
$return = "ERROR,Lamdaemon ($hostname),Unable to read quota!";
logMessage(LOG_ERR, "Unable to read quota for $user[0].");
}
else {
$return = "QUOTA_ENTRY $quota_usr[$i][1],$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$temp[7]:$return";
2009-06-14 12:17:48 +00:00
}
}
2011-02-12 15:30:22 +00:00
else { $return = "QUOTA_ENTRY $quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
2009-06-14 12:17:48 +00:00
}
2011-02-12 15:30:22 +00:00
else { $return = "QUOTA_ENTRY $quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
2009-06-14 12:17:48 +00:00
$i++;
}
($<, $>) = ($>, $<); # Give up root previleges
}
2008-07-21 20:06:11 +00:00
#
# Logs a message to the syslog.
#
# Parameters: level message
#
# level: error level
# message: message text
#
sub logMessage {
my $level = $_[0];
my $message = $_[1];
openlog('LAM - lamdaemon','','user');
syslog($level, $message);
closelog;
2009-06-14 11:36:38 +00:00
}