From 1efe9cbbdade4257b382891367b7b0fe86608590 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 11 Dec 2019 18:13:59 +0100 Subject: [Symlink] Rename and refit from LinkConfigTgz --- .../openslx/taskmanager/tasks/LinkConfigTgz.java | 73 ------------------- .../org/openslx/taskmanager/tasks/Symlink.java | 83 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java create mode 100644 src/main/java/org/openslx/taskmanager/tasks/Symlink.java diff --git a/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java b/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java deleted file mode 100644 index 7816247..0000000 --- a/src/main/java/org/openslx/taskmanager/tasks/LinkConfigTgz.java +++ /dev/null @@ -1,73 +0,0 @@ -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; - -import com.google.gson.annotations.Expose; - -public class LinkConfigTgz extends AbstractTask -{ - - protected static final String[] ALLOWED_DIRS = - { "/tmp/", "/opt/openslx/configs/" }; - - @Expose - private String destination = null; - - private Output status = new Output(); - - @Override - protected boolean initTask() - { - this.setStatusObject( status ); - if ( this.destination != null && !this.destination.isEmpty() ) { - this.destination = FilenameUtils.normalize( this.destination ); - if ( !Util.startsWith( this.destination, ALLOWED_DIRS ) ) { - status.error = "File not in allowed directory"; - return false; - } - } else { - this.destination = null; - } - return true; - } - - @Override - protected boolean execute() - { - try { - 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 ) { - } - if ( this.destination != null ) { - try { - Files.createSymbolicLink( Paths.get( "/srv/openslx/www/boot/default/config.tgz" ), Paths.get( this.destination ) ); - } catch ( IOException e ) { - status.error = e.toString(); - 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/Symlink.java b/src/main/java/org/openslx/taskmanager/tasks/Symlink.java new file mode 100644 index 0000000..7a6a3bd --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/Symlink.java @@ -0,0 +1,83 @@ +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; + +import com.google.gson.annotations.Expose; + +public class Symlink extends AbstractTask +{ + + protected static final String[] ALLOWED_DIRS = + { "/srv/openslx/www/boot/" }; + + @Expose + private String target = null; + @Expose + private String linkname = null; + + private Output status = new Output(); + + @Override + protected boolean initTask() + { + if ( Util.isEmpty( this.linkname ) ) { + status.error = "Link name empty"; + return false; + } + if ( this.linkname.endsWith( "/" ) ) { + status.error = "Link name ends with slash"; + return false; + } + this.setStatusObject( status ); + if ( Util.isEmpty( this.target ) ) { + status.error = "Target empty"; + return false; + } + this.linkname = FilenameUtils.normalize( this.linkname ); + if ( !Util.startsWith( this.linkname, ALLOWED_DIRS ) ) { + status.error = "Link name not in allowed directory"; + return false; + } + if ( !this.target.startsWith( "/" ) ) { + this.target = new File( this.linkname ).getParent() + "/" + this.target; + } + this.target = FilenameUtils.normalize( this.target ); + if ( !Util.startsWith( this.target, ALLOWED_DIRS ) ) { + status.error = "Target not in allowed directory"; + return false; + } + return true; + } + + @Override + protected boolean execute() + { + FileUtils.deleteQuietly( new File( linkname ) ); + if ( this.target != null ) { + try { + Files.createSymbolicLink( Paths.get( this.linkname ), Paths.get( this.target ) ); + } catch ( Exception e ) { + status.error = e.toString(); + return false; + } + } + return true; + } + + /** + * Output - contains additional status data of this task + */ + @SuppressWarnings( "unused" ) + private static class Output + { + protected String error = null; + } + +} -- cgit v1.2.3-55-g7522