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; /** * 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")); appendSetHomeACLProcessor(aCP, user); 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 -TERM -u " + uid + " && sleep 5 && killall -KILL -u " + uid + " && sleep 5 || true"); Processor userdelProc = null; if (user.isDefaultHomedir()) { userdelProc = new ShellProcessor("deluser --remove-home " + uid); } else { userdelProc = new ShellProcessor("deluser " + 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 appendSetHomeACLProcessor(CompoundProcessor aCP, UnixUser user) { aCP.appendProcessor(new ShellProcessor("chmod 700 " + user.getHomedir())); } 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; } 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" + "\"" )); } }