refactoring

This commit is contained in:
Roland Gruber 2009-06-14 11:36:38 +00:00
parent 0d986e228d
commit 99b258545d
1 changed files with 174 additions and 172 deletions

View File

@ -4,7 +4,7 @@
# #
# This code is part of LDAP Account Manager (http://www.sourceforge.net/projects/lam) # This code is part of LDAP Account Manager (http://www.sourceforge.net/projects/lam)
# Copyright (C) 2003 - 2006 Tilo Lutz # Copyright (C) 2003 - 2006 Tilo Lutz
# Copyright (C) 2006 - 2007 Roland Gruber # Copyright (C) 2006 - 2009 Roland Gruber
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -93,8 +93,15 @@ sub get_fs { # Load mountpoints from mtab if enabled quotas
} }
# ***************** Check values # ***************** Check values
if ($< == 0 ) { # we are root
# Drop root Previleges # check if script runs as root
if ($< != 0 ) {
print "ERROR,Lamdaemon ($hostname),Not called as root!\n";
logMessage(LOG_ERR, "Not called as root!");
exit 1;
}
# Drop root privileges
($<, $>) = ($>, $<); ($<, $>) = ($>, $<);
# loop for every transmitted user # loop for every transmitted user
while (1) { while (1) {
@ -119,36 +126,35 @@ if ($< == 0 ) { # we are root
# Get user information # Get user information
if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); } if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); }
else { @user = getgrnam($vals[0]); } else { @user = getgrnam($vals[0]); }
$vals[1] eq 'home' && do { if ($vals[1] eq 'home') {
switch2: { if ($vals[2] eq 'add') {
$vals[2] eq 'add' && do {
# split homedir to set all directories below the last dir. to 0755 # split homedir to set all directories below the last dir. to 0755
my $path = $user[7]; my $homedir = $user[7];
my $path = $homedir;
$path =~ s,/(?:[^/]*)$,,; $path =~ s,/(?:[^/]*)$,,;
($<, $>) = ($>, $<); # Get root privileges ($<, $>) = ($>, $<); # Get root privileges
if (! -e $path) { if (! -e $path) {
system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir
} }
if (! -e $user[7]) { if (! -e $homedir) {
system 'mkdir', '-m', $vals[3], $user[7]; # Create homedir itself system 'mkdir', '-m', $vals[3], $homedir; # Create homedir itself
system ("(cd /etc/skel && tar cf - .) | (cd $user[7] && tar xmf -)"); # Copy /etc/sekl into homedir system ("(cd /etc/skel && tar cf - .) | (cd $homedir && tar xmf -)"); # Copy /etc/sekl into homedir
system 'chown', '-hR', "$user[2]:$user[3]" , $user[7]; # Change owner to new user system 'chown', '-hR', "$user[2]:$user[3]" , $homedir; # Change owner to new user
if (-e '/usr/sbin/useradd.local') { if (-e '/usr/sbin/useradd.local') {
system '/usr/sbin/useradd.local', $user[0]; # run useradd-script system '/usr/sbin/useradd.local', $user[0]; # run useradd-script
system 'chmod', '-R', $vals[3], $user[7]; # Edit chmod rights system 'chmod', '-R', $vals[3], $homedir; # Edit chmod rights
} }
system 'chmod', $vals[3], $user[7]; # Edit chmod rights system 'chmod', $vals[3], $homedir; # Edit chmod rights
$return = "INFO,Lamdaemon ($hostname),Home directory created (" . $user[7] . ")."; $return = "INFO,Lamdaemon ($hostname),Home directory created (" . $homedir . ").";
logMessage(LOG_INFO, "Home directory created (" . $user[7] . ")"); logMessage(LOG_INFO, "Home directory created (" . $homedir . ")");
} }
else { else {
$return = "ERROR,Lamdaemon ($hostname),Home directory already exists (" . $user[7] . ")."; $return = "ERROR,Lamdaemon ($hostname),Home directory already exists (" . $homedir . ").";
logMessage(LOG_INFO, "Home directory already exists (" . $user[7] . ")"); logMessage(LOG_INFO, "Home directory already exists (" . $homedir . ")");
} }
($<, $>) = ($>, $<); # Give up root previleges ($<, $>) = ($>, $<); # Give up root previleges
last switch2; }
}; elsif ($vals[2] eq 'rem') {
$vals[2] eq 'rem' && do {
($<, $>) = ($>, $<); # Get root previliges ($<, $>) = ($>, $<); # Get root previliges
if (-d $user[7] && $user[7] ne '/') { if (-d $user[7] && $user[7] ne '/') {
if ((stat($user[7]))[4] eq $user[2]) { if ((stat($user[7]))[4] eq $user[2]) {
@ -168,15 +174,15 @@ if ($< == 0 ) { # we are root
$return = "INFO,Lamdaemon ($hostname),The directory which should be deleted was not found (skipped)."; $return = "INFO,Lamdaemon ($hostname),The directory which should be deleted was not found (skipped).";
} }
($<, $>) = ($>, $<); # Give up root previleges ($<, $>) = ($>, $<); # Give up root previleges
last switch2; }
}; else {
# Show error if undfined command is used # Show error if undefined command is used
$return = "ERROR,Lamdaemon ($hostname),Unknown command $vals[2]."; $return = "ERROR,Lamdaemon ($hostname),Unknown command $vals[2].";
logMessage(LOG_ERR, "Unknown command $vals[2]"); logMessage(LOG_ERR, "Unknown command $vals[2]");
} }
last switch; last switch;
}; };
$vals[1] eq 'quota' && do { if ($vals[1] eq 'quota') {
require Quota; # Needed to get and set quotas require Quota; # Needed to get and set quotas
get_fs(); # Load list of devices with enabled quotas get_fs(); # Load list of devices with enabled quotas
# Store quota information in array # Store quota information in array
@ -261,11 +267,6 @@ if ($< == 0 ) { # we are root
}; };
print "$return\n"; print "$return\n";
} }
}
else {
print "ERROR,Lamdaemon ($hostname),Not called as root!\n";
logMessage(LOG_ERR, "Not called as root!");
}
# #
# Logs a message to the syslog. # Logs a message to the syslog.
@ -282,3 +283,4 @@ sub logMessage {
syslog($level, $message); syslog($level, $message);
closelog; closelog;
} }