diff options
author | Simon Rettberg | 2015-01-23 19:19:58 +0100 |
---|---|---|
committer | Simon Rettberg | 2015-01-23 19:19:58 +0100 |
commit | 2eb5cea8578c2884c6f30fa379212bbfc1554bed (patch) | |
tree | 12d4eb781782e07e41b084444a2185a978e90c98 /src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java | |
parent | Pimp my boot menu (diff) | |
download | tmlite-bwlp-2eb5cea8578c2884c6f30fa379212bbfc1554bed.tar.gz tmlite-bwlp-2eb5cea8578c2884c6f30fa379212bbfc1554bed.tar.xz tmlite-bwlp-2eb5cea8578c2884c6f30fa379212bbfc1554bed.zip |
Stuff
Diffstat (limited to 'src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java')
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/SshdConfigGenerator.java | 83 |
1 files changed, 83 insertions, 0 deletions
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; + } + +} |