package de.hsadmin.mods.user; import javax.persistence.EntityManager; import de.hsadmin.core.model.AbstractEntity; import de.hsadmin.core.qserv.CompoundProcessor; import de.hsadmin.core.qserv.EntityProcessorFactory; import de.hsadmin.core.qserv.Processor; import de.hsadmin.core.qserv.ShellProcessor; import de.hsadmin.hostsharing.QuotaLimit; /** * Factory class which creates Processor instances for dealing with UNIX user * accounts. * * @author mi */ public class UnixUserProcessorFactory implements EntityProcessorFactory { /** * @return a Processor which creates a new UNIX user account */ public Processor createCreateProcessor(EntityManager em, 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 */ public Processor createUpdateProcessor(EntityManager em, 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 */ public Processor createDeleteProcessor(EntityManager em, T entity) { UnixUser user = (UnixUser) entity; String uid = user.getName(); if (uid != null && uid.length() > 4) { Processor killProcessesProc = new ShellProcessor("killall -u " + uid + " || echo -n 'no processes'"); Processor userdelProc = null; if (user.isDefaultHomedir()) { userdelProc = new ShellProcessor("userdel " + uid + " && rm -rf '" + user.getHomedir() + "'"); } else { userdelProc = new ShellProcessor("userdel " + uid); } CompoundProcessor proc = new CompoundProcessor(killProcessesProc, userdelProc); proc.appendProcessor(new ShellProcessor("rm -f '/var/spool/cron/crontabs/" + uid + "'")); proc.appendProcessor(new ShellProcessor("rm -f '/var/mail/" + uid + "'")); return proc; } return null; } private void appendSetQuotaProcessor(CompoundProcessor aCP, UnixUser user) { Integer quotaSoft = user.getQuotaSoftlimit(); if (quotaSoft == null) { quotaSoft = new Integer(0); } else { quotaSoft = quotaSoft * 1024; } if (quotaSoft.intValue() == 0) { 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); } else { quotaHard = quotaHard * 1024; } if (quotaHard.intValue() < quotaSoft.intValue()) { quotaHard = ((Double) (quotaSoft * QuotaLimit.USER_HARD_FACTOR)).intValue(); } aCP.appendProcessor(new ShellProcessor("setquota -u " + user.getName() + " " + quotaSoft + " " + quotaHard + " 0 0 " + "`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" + "\"" )); } }