From 2eb5cea8578c2884c6f30fa379212bbfc1554bed Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 23 Jan 2015 19:19:58 +0100 Subject: Stuff --- data/pxemenu.template | 2 +- data/sshd_config.template | 34 +++++++++ pom.xml | 16 +++- scripts/mount-store | 14 +++- scripts/system-backup | 1 + scripts/system-restore | 5 ++ src/main/java/org/openslx/satserver/util/Exec.java | 7 +- src/main/java/org/openslx/satserver/util/Util.java | 10 +++ .../taskmanager/tasks/BrandingGenerator.java | 9 ++- .../org/openslx/taskmanager/tasks/CompileIPxe.java | 86 +++++++++++++++++++--- .../openslx/taskmanager/tasks/CreateAdConfig.java | 4 +- .../openslx/taskmanager/tasks/DownloadFile.java | 7 +- .../openslx/taskmanager/tasks/DozmodLauncher.java | 2 +- .../org/openslx/taskmanager/tasks/LdapSearch.java | 1 + .../openslx/taskmanager/tasks/LighttpdHttps.java | 10 ++- .../openslx/taskmanager/tasks/LinkConfigTgz.java | 10 ++- .../taskmanager/tasks/LocalAddressesList.java | 2 +- .../org/openslx/taskmanager/tasks/MoveFile.java | 3 + .../org/openslx/taskmanager/tasks/SleepTask.java | 34 +++++++++ .../taskmanager/tasks/SshdConfigGenerator.java | 83 +++++++++++++++++++++ .../taskmanager/tasks/SyncdaemonLauncher.java | 2 +- 21 files changed, 306 insertions(+), 36 deletions(-) create mode 100644 data/sshd_config.template create mode 100644 src/main/java/org/openslx/taskmanager/tasks/SleepTask.java create mode 100644 src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java diff --git a/data/pxemenu.template b/data/pxemenu.template index 61ec1fa..5d39364 100644 --- a/data/pxemenu.template +++ b/data/pxemenu.template @@ -61,7 +61,7 @@ LABEL shutdown LABEL net MENU LABEL ^bwLehrpool-Umgebung starten TEXT HELP - Durch Drücken von ENTER wird die bwLehrpoolumgebung gestartet. + Durch Drücken von ENTER wird die bwLehrpool-Umgebung gestartet. Sie bietet Zugriff auf die Virtuellen Labore. ENDTEXT KERNEL http://%ipaddress%/boot/default/kernel diff --git a/data/sshd_config.template b/data/sshd_config.template new file mode 100644 index 0000000..0a73779 --- /dev/null +++ b/data/sshd_config.template @@ -0,0 +1,34 @@ +Port %PORT% +Protocol 2 +HostKey /etc/ssh/ssh_host_rsa_key +HostKey /etc/ssh/ssh_host_dsa_key +HostKey /etc/ssh/ssh_host_ecdsa_key +UsePrivilegeSeparation yes +KeyRegenerationInterval 3600 +ServerKeyBits 768 +SyslogFacility AUTH +LogLevel INFO +LoginGraceTime 30 +PermitRootLogin yes +StrictModes yes +RSAAuthentication yes +PubkeyAuthentication yes +PasswordAuthentication %PASSWORDLOGIN% +AuthorizedKeysFile %h/.ssh/authorized_keys +IgnoreRhosts yes +RhostsRSAAuthentication no +HostbasedAuthentication no +PermitEmptyPasswords no +ChallengeResponseAuthentication no +X11Forwarding yes +X11DisplayOffset 10 +PrintMotd no +PrintLastLog yes +TCPKeepAlive yes +Banner /etc/issue.net +AcceptEnv LANG LC_* +Subsystem sftp /usr/lib/openssh/sftp-server +UsePAM yes +DenyUsers demo +UseDNS no + diff --git a/pom.xml b/pom.xml index 5ced499..07a5c02 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,14 @@ UTF-8 + + + mltk-repo + mltk repo + http://mltk-services.ruf.uni-freiburg.de:8081/nexus/content/repositories/snapshots/ + + + @@ -75,15 +83,15 @@ compile - com.kitfox.svg - svg-salamander - 1.0 + com.kitfox + kitfox-svg-salamander + 1.0.8 org.openslx.bwlp master-sync-shared 1.0-SNAPSHOT compile - + diff --git a/scripts/mount-store b/scripts/mount-store index cd31952..f138c5f 100755 --- a/scripts/mount-store +++ b/scripts/mount-store @@ -60,14 +60,20 @@ touch "${DEST}/.notmounted" if grep -E -q '^[^/].+:.+' <<<$SOURCE; then # seems to be NFS - mount -t nfs -o rw,async,nolock,vers=3,fg,ac,retry=1,timeo=600 "$SOURCE" "$DEST" - RET=$? + for i in 1 2 3; do + mount -t nfs -o rw,async,nolock,vers=3,fg,ac,retry=1,timeo=100,sec=sys "$SOURCE" "$DEST" + RET=$? + [ "$RET" -eq "0" ] && break + done elif grep -E -q '^//' <<<$SOURCE; then # seens to be SMB export USER="$USERNAME" export PASSWD="$PASSWORD" - mount -t cifs -o rw,uid=0,gid=12345,forceuid,forcegid,file_mode=0664,dir_mode=0775,sec=ntlm "$SOURCE" "$DEST" - RET=$? + for sec in ntlmv2 ntlm; do + mount -t cifs -o rw,uid=0,gid=12345,forceuid,forcegid,file_mode=0664,dir_mode=0775,sec=$sec "$SOURCE" "$DEST" + RET=$? + [ "$RET" -eq "0" ] && break + done unset USER PASSWD else echo "Unknown mount type: $SOURCE" diff --git a/scripts/system-backup b/scripts/system-backup index 21f791b..d4b9ebe 100755 --- a/scripts/system-backup +++ b/scripts/system-backup @@ -27,6 +27,7 @@ FILELIST=" /opt/syncdaemon/config/identity.properties /etc/lighttpd/server.pem /etc/lighttpd/chain.pem + /srv/openslx/www/boot/default/config.tgz " tar --ignore-failed-read -k -c -p -z -f files.tgz $FILELIST # no quotes here! diff --git a/scripts/system-restore b/scripts/system-restore index a95a185..5d77dda 100755 --- a/scripts/system-restore +++ b/scripts/system-restore @@ -45,7 +45,12 @@ if [ $RET -ne 0 ]; then echo "Error: Restoring database contents failed with exit code $RET" exit 1 fi +# Since we came that far we'll delete some old configs (if existent) +rm -rf /opt/ldadp/{configs,pid,logs}/* /opt/openslx/configs/* /srv/openslx/www/boot/default/config.tgz 2> /dev/null +# Force triggering IP detection/setting, which should in turn regenerate ldadp configs and launch ldadp instances if applicable echo "UPDATE openslx.property SET value = 'invalid' WHERE name = 'server-ip' LIMIT 1" | mysql --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 +# Try to update the db (if required) +curl -s 'http://localhost/slx-admin/api.php?do=update' tar --ignore-failed-read -x -f files.tgz -C / RET=$? diff --git a/src/main/java/org/openslx/satserver/util/Exec.java b/src/main/java/org/openslx/satserver/util/Exec.java index 1f810eb..db59b0d 100644 --- a/src/main/java/org/openslx/satserver/util/Exec.java +++ b/src/main/java/org/openslx/satserver/util/Exec.java @@ -13,9 +13,14 @@ public class Exec * @return exit code */ public static int sync( String... command ) + { + return syncAt( "/", command ); + } + + public static int syncAt( String cwd, String... command ) { ProcessBuilder pb = new ProcessBuilder( command ); - pb.directory( new File( "/" ) ); + pb.directory( new File( cwd ) ); Process p; try { p = pb.start(); diff --git a/src/main/java/org/openslx/satserver/util/Util.java b/src/main/java/org/openslx/satserver/util/Util.java index 2df4c73..fcf10ed 100644 --- a/src/main/java/org/openslx/satserver/util/Util.java +++ b/src/main/java/org/openslx/satserver/util/Util.java @@ -22,6 +22,8 @@ public class Util */ public static boolean startsWith( String stringToCheck, String... compareTo ) { + if ( stringToCheck == null ) + return false; for ( String check : compareTo ) { if ( stringToCheck.startsWith( check ) ) return true; @@ -77,4 +79,12 @@ public class Util FileUtils.writeStringToFile( file, string, StandardCharsets.UTF_8 ); } + private static final String[] DEFAULT_ALLOWED_DIRS = + { "/tmp/", "/opt/openslx/configs/" }; + + public static boolean isAllowedDir( String dir ) + { + return startsWith( dir, DEFAULT_ALLOWED_DIRS ); + } + } diff --git a/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java b/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java index add2351..7192eba 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java +++ b/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java @@ -29,9 +29,6 @@ public class BrandingGenerator extends AbstractTask @Expose private String svgFile = null; - protected static final String[] ALLOWED_DIRS = - { "/tmp/", "/opt/openslx/configs/" }; - private Output status = new Output(); @Override @@ -48,7 +45,7 @@ public class BrandingGenerator extends AbstractTask } this.svgFile = FilenameUtils.normalize( this.svgFile ); this.tarFile = FilenameUtils.normalize( this.tarFile ); - if ( !Util.startsWith( this.svgFile, ALLOWED_DIRS ) || !Util.startsWith( this.tarFile, ALLOWED_DIRS ) ) { + if ( !Util.isAllowedDir( this.svgFile ) || !Util.isAllowedDir( this.tarFile ) ) { status.error = "svgFile or tarFile not in allowed dir"; return false; } @@ -75,6 +72,10 @@ public class BrandingGenerator extends AbstractTask rasterImage.setClip( 0, 0, bi.getWidth(), bi.getHeight() ); //rasterImage.setBackground( Color.WHITE ); rasterImage.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); + rasterImage.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); + rasterImage.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR ); + rasterImage.setRenderingHint( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY ); + rasterImage.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON ); AffineTransform at = new AffineTransform(); at.setToScale( bi.getWidth() / diagram.getWidth(), bi.getWidth() / diagram.getWidth() ); rasterImage.transform( at ); diff --git a/src/main/java/org/openslx/taskmanager/tasks/CompileIPxe.java b/src/main/java/org/openslx/taskmanager/tasks/CompileIPxe.java index 741c71e..a5b436f 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/CompileIPxe.java +++ b/src/main/java/org/openslx/taskmanager/tasks/CompileIPxe.java @@ -2,15 +2,21 @@ package org.openslx.taskmanager.tasks; import java.io.File; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; +import org.openslx.satserver.util.Exec; import org.openslx.taskmanager.api.AbstractTask; import com.google.gson.annotations.Expose; public class CompileIPxe extends AbstractTask { + private static final Logger LOG = Logger.getLogger( CompileIPxe.class ); @Expose private String defaultentry = null; @@ -18,6 +24,10 @@ public class CompileIPxe extends AbstractTask private int timeout = 0; @Expose private String custom = null; + @Expose + private String ipaddress = null; + @Expose + private String masterpassword = null; private Output status = new Output(); @@ -25,15 +35,31 @@ public class CompileIPxe extends AbstractTask protected boolean initTask() { this.setStatusObject( this.status ); + if ( this.ipaddress == null || this.ipaddress.isEmpty() ) { + status.error = "No IP address given!"; + return false; + } if ( this.defaultentry == null ) this.defaultentry = "net"; if ( this.custom == null ) this.custom = ""; + if ( this.masterpassword == null ) + this.masterpassword = ""; return true; } @Override protected boolean execute() + { + boolean ret = true; + if ( !updateMenu() ) + ret = false; + if ( !updateIpxe() ) + ret = false; + return ret; + } + + private boolean updateMenu() { // Prepare menu String template; @@ -44,27 +70,69 @@ public class CompileIPxe extends AbstractTask return false; } // Substitutions - template = template.replaceAll( "%timeout%", Integer.toString( this.timeout * 10 ) ); - template = template.replaceAll( "%totaltimeout%", Integer.toString( this.timeout * 40 ) ); - template = template.replaceAll( "%default%", this.defaultentry ); - template = template.replaceAll( "%custom%", this.custom ); + template = template.replace( "%timeout%", Integer.toString( this.timeout * 10 ) ); + template = template.replace( "%totaltimeout%", Integer.toString( this.timeout * 40 ) ); + template = template.replace( "%default%", this.defaultentry ); + template = template.replace( "%custom%", this.custom ); + template = template.replace( "%ipaddress%", this.ipaddress ); + template = template.replace( "%masterpassword%", this.masterpassword ); // Default selection net if ( this.defaultentry.equals( "net" ) ) - template = template.replaceAll( "%default-net%", "MENU DEFAULT" ); + template = template.replace( "%default-net%", "MENU DEFAULT" ); else - template = template.replaceAll( "%default-net%", "" ); + template = template.replace( "%default-net%", "" ); // Default selection hdd if ( this.defaultentry.equals( "hdd" ) ) - template = template.replaceAll( "%default-hdd%", "MENU DEFAULT" ); + template = template.replace( "%default-hdd%", "MENU DEFAULT" ); else - template = template.replaceAll( "%default-hdd%", "" ); + template = template.replace( "%default-hdd%", "" ); + // Write out + try { + Charset cs; + if ( Charset.isSupported( "IBM437" ) ) + cs = Charset.forName( "IBM437" ); + else if ( Charset.isSupported( "Cp437" ) ) + cs = Charset.forName( "Cp437" ); + else + cs = StandardCharsets.UTF_8; + FileUtils.writeStringToFile( new File( "/srv/openslx/tftp/pxelinux.cfg/default" ), template, cs ); + } catch ( IOException e ) { + status.error = e.toString(); + return false; + } + return true; + } + + private boolean updateIpxe() + { + // Prepare menu + String template; + try { + template = FileUtils.readFileToString( new File( "./data/ipxe-embed.template" ), StandardCharsets.UTF_8 ); + } catch ( IOException e ) { + status.error = e.toString(); + return false; + } + // Substitution + template = template.replace( "%ipaddress%", this.ipaddress ); // Write out try { - FileUtils.writeStringToFile( new File( "/srv/openslx/tftp/pxelinux.cfg/default" ), template, StandardCharsets.UTF_8 ); + FileUtils.writeStringToFile( new File( "/opt/openslx/ipxe/ipxelinux.ipxe" ), template, StandardCharsets.UTF_8 ); } catch ( IOException e ) { status.error = e.toString(); return false; } + // Compile + if ( 0 != Exec.syncAt( "/opt/openslx/ipxe/src", "make", "EMBED=../ipxelinux.ipxe,../pxelinux.0", "bin/undionly.kkkpxe" ) ) { + status.error = "Compiling ipxelinux.0 failed"; + return false; + } + try { + FileUtils.copyFile( new File( "/opt/openslx/ipxe/src/bin/undionly.kkkpxe" ), new File( "/srv/openslx/tftp/v4/ipxelinux.0" ) ); + } catch ( Exception e ) { + status.error = e.toString(); + return false; + } return true; } diff --git a/src/main/java/org/openslx/taskmanager/tasks/CreateAdConfig.java b/src/main/java/org/openslx/taskmanager/tasks/CreateAdConfig.java index 67d6099..c74b9dc 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/CreateAdConfig.java +++ b/src/main/java/org/openslx/taskmanager/tasks/CreateAdConfig.java @@ -43,12 +43,14 @@ public class CreateAdConfig extends AbstractTask { // TODO: Check path is allowed this.setStatusObject( this.status ); - if ( filename == null || server == null || searchbase == null || binddn == null || bindpw == null || proxyip == null || proxyport == 0 || moduleid == 0 ) { + if ( filename == null || server == null || searchbase == null || binddn == null || proxyip == null || proxyport == 0 || moduleid == 0 ) { status.error = "Missing argument to task"; return false; } if ( this.home == null ) this.home = ""; + if ( this.bindpw == null ) + this.bindpw = ""; return true; } diff --git a/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java b/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java index 1129200..dc36cfc 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DownloadFile.java @@ -6,7 +6,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; -import java.nio.charset.StandardCharsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; @@ -78,7 +77,7 @@ public class DownloadFile extends AbstractTask } fout.close(); // If we have a gpg sig, validate - if ( this.gpg != null ) { + if ( this.gpg != null && !this.gpg.isEmpty() ) { File gpgTempFile = null; try { gpgTempFile = File.createTempFile( "bwlp-", ".gpg", null ); @@ -87,8 +86,8 @@ public class DownloadFile extends AbstractTask status.error = "Could not create temporary file for gpg signature"; return false; } - if ( 0 != Exec.sync( "gpg", "--homedir", "/opt/openslx/gpg", "--verify", gpgTempFile.getAbsolutePath(), tmpFile.getAbsolutePath() ) ) { - status.error = "GPG signature of downloaded file not valid!"; + if ( 0 != Exec.sync( "gpg", "--verify", gpgTempFile.getAbsolutePath(), tmpFile.getAbsolutePath() ) ) { + status.error = "GPG signature of downloaded file not valid!\n\n" + this.gpg; return false; } gpgTempFile.delete(); diff --git a/src/main/java/org/openslx/taskmanager/tasks/DozmodLauncher.java b/src/main/java/org/openslx/taskmanager/tasks/DozmodLauncher.java index a577702..8c8720d 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DozmodLauncher.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DozmodLauncher.java @@ -16,7 +16,7 @@ public class DozmodLauncher extends SystemCommandTask @Expose private String operation; - private StatusObject status = null; + private StatusObject status = new StatusObject(); @Override protected String[] initCommandLine() diff --git a/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java b/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java index dbb76b1..e1fec55 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java +++ b/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java @@ -77,6 +77,7 @@ public class LdapSearch extends SystemCommandTask "-b", this.searchbase, // SB "-D", this.binddn, // DN "-l", "4", // Time limit in seconds + "-o", "nettimeout=4", "-z", "4", // Max number of results "-o", "ldif-wrap=no", // Turn off retarded line wrapping done by ldapsearch "(&(objectClass=user)(objectClass=person)(sAMAccountName=" + this.username + "))", diff --git a/src/main/java/org/openslx/taskmanager/tasks/LighttpdHttps.java b/src/main/java/org/openslx/taskmanager/tasks/LighttpdHttps.java index e3a06bf..6324657 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/LighttpdHttps.java +++ b/src/main/java/org/openslx/taskmanager/tasks/LighttpdHttps.java @@ -8,6 +8,10 @@ import org.openslx.taskmanager.api.AbstractTask; import com.google.gson.annotations.Expose; +/** + * Task for enabling or disabling https support in lighttpd. + * Can greate a self-signed cert on the fly, or use a supplied one. + */ public class LighttpdHttps extends AbstractTask { @@ -33,9 +37,9 @@ public class LighttpdHttps extends AbstractTask @Override protected boolean execute() { - if ( this.importcert != null && this.importkey != null ) + if ( this.importcert != null && this.importkey != null && !this.importcert.isEmpty() && !this.importkey.isEmpty() ) return createFromInput(); - if ( this.proxyip != null ) + if ( this.proxyip != null && !this.proxyip.isEmpty() ) return createRandom(); return disableHttps(); } @@ -99,7 +103,7 @@ public class LighttpdHttps extends AbstractTask { int ret = Exec.sync( "sudo", "-n", "-u", "root", "/opt/taskmanager/scripts/install-https", "--disable" ); if ( ret != 0 ) { - status.error = "import exited with code " + ret; + status.error = "disable exited with code " + ret; return false; } return true; diff --git a/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java b/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java index 686cb9b..bd770d5 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java +++ b/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java @@ -1,9 +1,11 @@ package org.openslx.taskmanager.tasks; +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.AbstractTask; @@ -37,8 +39,12 @@ public class LinkConfigTgz extends AbstractTask protected boolean execute() { try { - Files.deleteIfExists( Paths.get( "/srv/openslx/www/boot/default/config.tgz" ) ); - } catch ( IOException e1 ) { + Files.createDirectory( Paths.get( "/srv/openslx/www/boot/default" ) ); + } catch (Exception e) { + } + try { + FileUtils.deleteQuietly( new File( "/srv/openslx/www/boot/default/config.tgz" ) ); + } catch (Exception e) { } try { Files.createSymbolicLink( Paths.get( "/srv/openslx/www/boot/default/config.tgz" ), Paths.get( this.destination ) ); diff --git a/src/main/java/org/openslx/taskmanager/tasks/LocalAddressesList.java b/src/main/java/org/openslx/taskmanager/tasks/LocalAddressesList.java index 2bfb20e..ad5d539 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/LocalAddressesList.java +++ b/src/main/java/org/openslx/taskmanager/tasks/LocalAddressesList.java @@ -36,7 +36,7 @@ public class LocalAddressesList extends AbstractTask InetAddress addr = enumIpAddr.nextElement(); Output.Entry entry = new Output.Entry(); entry.iface = intf.getName(); - entry.ip = addr.getHostAddress(); + entry.ip = addr.getHostAddress().replaceFirst( "%\\d+$", "" ); if ( addr instanceof Inet4Address ) { entry.type = "ipv4"; } else if ( addr instanceof Inet6Address ) { diff --git a/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java b/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java index f783019..e01cf87 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java +++ b/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java @@ -1,8 +1,10 @@ package org.openslx.taskmanager.tasks; +import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.openslx.satserver.util.Util; import org.openslx.taskmanager.api.AbstractTask; @@ -43,6 +45,7 @@ public class MoveFile extends AbstractTask protected boolean execute() { try { + FileUtils.deleteQuietly( new File( this.destination ) ); Files.move( Paths.get( this.source ), Paths.get( this.destination ) ); } catch ( Exception e1 ) { status.error = e1.toString(); diff --git a/src/main/java/org/openslx/taskmanager/tasks/SleepTask.java b/src/main/java/org/openslx/taskmanager/tasks/SleepTask.java new file mode 100644 index 0000000..77c7a80 --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/SleepTask.java @@ -0,0 +1,34 @@ +package org.openslx.taskmanager.tasks; + +import org.openslx.taskmanager.api.AbstractTask; + +import com.google.gson.annotations.Expose; + +/** + * Sleep Task that will just do what the name says. + * Useful only when chaining tasks and you want a pause in between. + */ +public class SleepTask extends AbstractTask +{ + + @Expose + private int seconds = 0; + + @Override + protected boolean initTask() + { + return true; + } + + @Override + protected boolean execute() + { + try { + Thread.sleep( this.seconds * 1000 ); + } catch ( InterruptedException e ) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java b/src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java new file mode 100644 index 0000000..09c32f7 --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java @@ -0,0 +1,83 @@ +package org.openslx.taskmanager.tasks; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.io.FileUtils; +import org.openslx.satserver.util.Archive; +import org.openslx.satserver.util.Util; +import org.openslx.taskmanager.api.AbstractTask; + +import com.google.gson.annotations.Expose; + +public class SshdConfigGenerator extends AbstractTask +{ + @Expose + private int listenPort = 0; + @Expose + private String allowPasswordLogin = "no"; + @Expose + private String publicKey = ""; + @Expose + private String filename = null; + + private Output status = new Output(); + + @Override + protected boolean initTask() + { + this.setStatusObject( status ); + if ( !Util.isAllowedDir( this.filename ) ) + status.error = "Invalid directory for " + this.filename; + if ( allowPasswordLogin == null || ( !allowPasswordLogin.equals( "yes" ) && !allowPasswordLogin.equals( "no" ) ) ) + status.error = "Invalid value for allowPasswordLogin: " + allowPasswordLogin; + if ( listenPort > 65535 || listenPort < 1 ) + status.error = "Invalid value for listenPort: " + listenPort; + if ( publicKey != null && !publicKey.isEmpty() && !publicKey.matches( "^[a-z0-9\\-]+ [A-Za-z0-9=/\\+]+ " ) ) + status.error = "Invalid public key: '" + publicKey + "'"; + return status.error == null; + } + + @Override + protected boolean execute() + { + TarArchiveOutputStream outArchive = null; + // Prepare sshd config + String template; + try { + template = FileUtils.readFileToString( new File( "./data/sshd_config.template" ), StandardCharsets.UTF_8 ); + } catch ( IOException e ) { + status.error = e.toString(); + return false; + } + template.replace( "%PORT%", Integer.toString( this.listenPort ) ); + template.replace( "%PASSWORDLOGIN%", allowPasswordLogin ); + try { + outArchive = Archive.createTarArchive( this.filename ); + } catch ( IOException e ) { + status.error = "Could not create archive at " + this.filename; + return false; + } + boolean ok = Archive.tarCreateFileFromString( outArchive, "/etc/ssh/sshd_config", template, 0644 ); + if ( publicKey != null && !publicKey.isEmpty() ) + ok |= Archive.tarCreateFileFromString( outArchive, "/root/.ssh/authorized_keys", publicKey, 0600 ); + ok |= Archive.tarCreateSymlink( outArchive, "../sshd.service", "/etc/systemd/system/network.target.wants/sshd.service" ); + if ( !ok ) { + status.error = "Could not create module archive contents"; + return false; + } + return true; + } + + /** + * Output - contains additional status data of this task + */ + @SuppressWarnings( "unused" ) + private static class Output + { + protected String error = null; + } + +} diff --git a/src/main/java/org/openslx/taskmanager/tasks/SyncdaemonLauncher.java b/src/main/java/org/openslx/taskmanager/tasks/SyncdaemonLauncher.java index 8e15919..22c092d 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/SyncdaemonLauncher.java +++ b/src/main/java/org/openslx/taskmanager/tasks/SyncdaemonLauncher.java @@ -26,7 +26,7 @@ public class SyncdaemonLauncher extends SystemCommandTask @Expose private String address; - private StatusObject status = null; + private StatusObject status = new StatusObject(); @Override protected String[] initCommandLine() -- cgit v1.2.3-55-g7522