diff options
author | Simon Rettberg | 2014-06-03 16:47:36 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-06-03 16:47:36 +0200 |
commit | 32dc5354e2916387a2c62eadae0a4568023f1151 (patch) | |
tree | 7fd9a0173d6073e86d1d48e545646b1bc8c1a5eb /src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java | |
download | tmlite-bwlp-32dc5354e2916387a2c62eadae0a4568023f1151.tar.gz tmlite-bwlp-32dc5354e2916387a2c62eadae0a4568023f1151.tar.xz tmlite-bwlp-32dc5354e2916387a2c62eadae0a4568023f1151.zip |
Initial commit
Diffstat (limited to 'src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java')
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java b/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java new file mode 100644 index 0000000..f861b6a --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/LdapSearch.java @@ -0,0 +1,122 @@ +package org.openslx.taskmanager.tasks; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Random; + +import org.apache.commons.io.FileUtils; +import org.openslx.taskmanager.api.SystemCommandTask; + +import com.google.gson.annotations.Expose; + +public class LdapSearch extends SystemCommandTask +{ + + @Expose + private String server = null; + @Expose + private String searchbase = null; + @Expose + private String binddn = null; + @Expose + private String bindpw = null; + + private String fifo = null; + + private volatile int userCount = 0; + + private Output status = new Output(); + + @Override + protected boolean initTask() + { + this.setStatusObject( this.status ); + if ( this.server == null || this.searchbase == null || this.binddn == null ) { + status.messages = "Missing parameter"; + return false; + } + return true; + } + + @Override + protected String[] initCommandLine() + { + if ( this.bindpw == null ) + this.bindpw = ""; + this.fifo = String.format( "/tmp/bwlp-%s-%s.ldap", System.currentTimeMillis(), new Random().nextInt() ); + File pwFile = new File( this.fifo ); + FileUtils.deleteQuietly( pwFile ); + try { + pwFile.createNewFile(); + pwFile.setReadable( false, false ); + pwFile.setReadable( true, true ); + FileUtils.writeStringToFile( pwFile, this.bindpw, StandardCharsets.UTF_8 ); + } catch ( IOException e ) { + FileUtils.deleteQuietly( pwFile ); + status.messages = e.toString(); + return null; + } + status.addMessage( "Trying to find 4 random AD users to verify everything is all right..." ); + + return new String[] { + "ldapsearch", + "-x", // Simple auth + "-LLL", // No additional stuff + "-y", this.fifo, // Password from file + "-H", "ldap://" + this.server + ":3268/", // Host + "-b", this.searchbase, // SB + "-D", this.binddn, // DN + "-l", "4", // Time limit in seconds + "-z", "4", // Max number of results + "-o", "ldif-wrap=no", // Turn off retarded line wrapping done by ldapsearch + "(&(objectClass=user)(objectClass=person)(sAMAccountName=*))", + "sAMAccountName" // Only one attribute + }; + } + + @Override + protected boolean processEnded( int exitCode ) + { + FileUtils.deleteQuietly( new File( this.fifo ) ); + if ( exitCode == 4 ) // Means size limit exceeded, ignore + exitCode = 0; + if ( exitCode != 0 ) + status.addMessage( "Exit code is " + exitCode ); + if ( exitCode == 0 && this.userCount < 4 ) + status.addMessage( "Found less than 4 users. Are you sure you got the right credentials." ); + return this.userCount >= 4; + } + + @Override + protected void processStdOut( String line ) + { + if ( line.contains( "sAMAccountName: " ) ) { + status.addMessage( "Found AD user " + line.substring( 16 ) + " :-)" ); + this.userCount++; + } + } + + @Override + protected void processStdErr( String line ) + { + if ( line.contains( "Size limit exceeded" ) ) + return; + status.addMessage( "Error: " + line ); + } + + class Output + { + private String messages = null; + + private void addMessage( String str ) + { + if ( messages == null ) { + messages = str; + } else { + messages += "\n" + str; + } + } + } + +} |