2010-10-01 21:52:51 +02:00
|
|
|
package de.hsadmin.mods.user;
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
2010-10-04 19:44:49 +02:00
|
|
|
import de.hsadmin.core.model.AbstractEntity;
|
2010-10-01 21:52:51 +02:00
|
|
|
import de.hsadmin.core.qserv.CompoundProcessor;
|
|
|
|
import de.hsadmin.core.qserv.EntityProcessorFactory;
|
|
|
|
import de.hsadmin.core.qserv.Processor;
|
|
|
|
import de.hsadmin.core.qserv.ShellProcessor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory class which creates Processor instances for dealing with UNIX user
|
|
|
|
* accounts.
|
|
|
|
*
|
|
|
|
* @author mi
|
|
|
|
*/
|
|
|
|
public class UnixUserProcessorFactory implements EntityProcessorFactory {
|
2013-06-19 14:50:45 +02:00
|
|
|
|
2010-10-01 21:52:51 +02:00
|
|
|
/**
|
|
|
|
* @return a Processor which creates a new UNIX user account
|
|
|
|
*/
|
2010-10-04 19:44:49 +02:00
|
|
|
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em,
|
2010-10-01 21:52:51 +02:00
|
|
|
T entity) {
|
|
|
|
UnixUser user = (UnixUser) entity;
|
|
|
|
CompoundProcessor aCP = new CompoundProcessor(new ShellProcessor(
|
|
|
|
"newusers", user.getName() + ":" + user.getPassword() + ":"
|
|
|
|
+ user.getUserId() + ":" + user.getPac().getName()
|
|
|
|
+ ":" + user.getComment() + ":" + user.getHomedir()
|
|
|
|
+ ":" + user.getShell() + "\n"));
|
|
|
|
appendSetQuotaProcessor(aCP, user);
|
|
|
|
appendMakeMaildirProcessor(aCP, user);
|
|
|
|
return aCP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a Processor which updates an existing UNIX user account
|
|
|
|
*/
|
2010-10-04 19:44:49 +02:00
|
|
|
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em,
|
2010-10-01 21:52:51 +02:00
|
|
|
T entity) {
|
|
|
|
UnixUser user = (UnixUser) entity;
|
|
|
|
CompoundProcessor aCP = new CompoundProcessor(new ShellProcessor(
|
|
|
|
"usermod -c '" + user.getComment() + "'" + " -d '"
|
|
|
|
+ user.getHomedir() + "'" + " -s '" + user.getShell()
|
|
|
|
+ "' " + user.getName()));
|
|
|
|
if (user.getPassword() != null && user.getPassword().length() > 0)
|
|
|
|
aCP.appendProcessor(new ShellProcessor("chpasswd ", user.getName()
|
|
|
|
+ ":" + user.getPassword() + "\n"));
|
|
|
|
appendSetQuotaProcessor(aCP, user);
|
|
|
|
return aCP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a Processor which deletes an existing UNIX user account
|
|
|
|
*/
|
2011-05-17 17:04:06 +02:00
|
|
|
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
|
2010-10-01 21:52:51 +02:00
|
|
|
UnixUser user = (UnixUser) entity;
|
2011-05-17 17:04:06 +02:00
|
|
|
String uid = user.getName();
|
|
|
|
if (uid != null && uid.length() > 4) {
|
2014-08-28 16:57:15 +02:00
|
|
|
Processor killProcessesProc = new ShellProcessor("killall -TERM -u " + uid + " && sleep 5 && killall -KILL -u " + uid + " && sleep 5 || true");
|
2011-05-17 17:04:06 +02:00
|
|
|
Processor userdelProc = null;
|
|
|
|
if (user.isDefaultHomedir()) {
|
2014-08-28 17:45:21 +02:00
|
|
|
userdelProc = new ShellProcessor("deluser --remove-home " + uid);
|
2011-05-17 17:04:06 +02:00
|
|
|
} else {
|
2014-08-28 12:40:35 +02:00
|
|
|
userdelProc = new ShellProcessor("deluser " + uid);
|
2011-05-17 17:04:06 +02:00
|
|
|
}
|
2011-10-28 12:24:24 +02:00
|
|
|
CompoundProcessor proc = new CompoundProcessor(killProcessesProc, userdelProc);
|
2011-05-17 19:33:48 +02:00
|
|
|
proc.appendProcessor(new ShellProcessor("rm -f '/var/spool/cron/crontabs/" + uid + "'"));
|
|
|
|
proc.appendProcessor(new ShellProcessor("rm -f '/var/mail/" + uid + "'"));
|
2011-05-17 17:04:06 +02:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
return null;
|
2010-10-01 21:52:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void appendSetQuotaProcessor(CompoundProcessor aCP, UnixUser user) {
|
|
|
|
Integer quotaSoft = user.getQuotaSoftlimit();
|
2013-06-19 14:50:45 +02:00
|
|
|
if (quotaSoft == null) {
|
|
|
|
quotaSoft = new Integer(0);
|
|
|
|
} else {
|
|
|
|
quotaSoft = quotaSoft * 1024;
|
|
|
|
}
|
|
|
|
if (quotaSoft.intValue() == 0) {
|
2010-10-01 21:52:51 +02:00
|
|
|
aCP.appendProcessor(new ShellProcessor("setquota -u "
|
|
|
|
+ user.getName() + " 0 0 0 0 "
|
|
|
|
+ "`df /home/pacs/ | tail -n1 | cut -d' ' -f1`"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Integer quotaHard = user.getQuotaHardlimit();
|
|
|
|
if (quotaHard == null) {
|
|
|
|
quotaHard = new Integer(0);
|
2013-06-19 14:50:45 +02:00
|
|
|
} else {
|
|
|
|
quotaHard = quotaHard * 1024;
|
2013-07-15 18:23:46 +02:00
|
|
|
}
|
2010-10-01 21:52:51 +02:00
|
|
|
aCP.appendProcessor(new ShellProcessor("setquota -u "
|
2013-06-19 14:50:45 +02:00
|
|
|
+ user.getName() + " " + quotaSoft + " "
|
|
|
|
+ quotaHard + " 0 0 "
|
2010-10-01 21:52:51 +02:00
|
|
|
+ "`df /home/pacs/ | tail -n1 | cut -d' ' -f1`"));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void appendMakeMaildirProcessor(CompoundProcessor aCP, UnixUser user) {
|
|
|
|
aCP.appendProcessor(
|
|
|
|
new ShellProcessor(
|
|
|
|
"su -l " + user.getName() + " -s \"/bin/bash\" -c \"maildirmake " +
|
|
|
|
user.getHomedir() + "/Maildir" +
|
|
|
|
"\""
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|