diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-02-02 15:10:03 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-02-02 15:10:03 +0530 |
| commit | dfd14c05bc117df8de627c48d4615fc9d995bbe0 (patch) | |
| tree | a1d1d7d043a102d8904cb5ade021c1937172a523 /com.gluster.storage.management.core | |
initial commit
Diffstat (limited to 'com.gluster.storage.management.core')
36 files changed, 3614 insertions, 0 deletions
diff --git a/com.gluster.storage.management.core/.classpath b/com.gluster.storage.management.core/.classpath new file mode 100644 index 00000000..1bdfad39 --- /dev/null +++ b/com.gluster.storage.management.core/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/com.gluster.storage.management.core/.project b/com.gluster.storage.management.core/.project new file mode 100644 index 00000000..6c8fbb5c --- /dev/null +++ b/com.gluster.storage.management.core/.project @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>com.gluster.storage.management.core</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/com.gluster.storage.management.core/.settings/org.eclipse.jdt.core.prefs b/com.gluster.storage.management.core/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..f22cedf1 --- /dev/null +++ b/com.gluster.storage.management.core/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Wed Dec 22 11:18:01 IST 2010 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/com.gluster.storage.management.core/META-INF/MANIFEST.MF b/com.gluster.storage.management.core/META-INF/MANIFEST.MF new file mode 100644 index 00000000..b5a2a830 --- /dev/null +++ b/com.gluster.storage.management.core/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Gluster Storage Platform Core +Bundle-SymbolicName: com.gluster.storage.management.core +Bundle-Version: 1.0.0 +Bundle-Vendor: GLUSTER +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Export-Package: com.gluster.storage.management.core.constants, + com.gluster.storage.management.core.exceptions, + com.gluster.storage.management.core.model, + com.gluster.storage.management.core.utils +Require-Bundle: org.eclipse.core.runtime;bundle-version="3.6.0" diff --git a/com.gluster.storage.management.core/build.properties b/com.gluster.storage.management.core/build.properties new file mode 100644 index 00000000..34d2e4d2 --- /dev/null +++ b/com.gluster.storage.management.core/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/constants/CoreConstants.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/constants/CoreConstants.java new file mode 100644 index 00000000..bb8f0b33 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/constants/CoreConstants.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + * This file is part of GlusterFS. + * + * Gluster Storage Platform is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, + * or (at your option) any later version. + * + * GlusterFS is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ + +package com.gluster.storage.management.core.constants; + +/** + * + */ +public class CoreConstants { + public static final String NEWLINE = System.getProperty("line.separator"); +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/exceptions/GlusterRuntimeException.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/exceptions/GlusterRuntimeException.java new file mode 100644 index 00000000..cf815989 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/exceptions/GlusterRuntimeException.java @@ -0,0 +1,12 @@ +package com.gluster.storage.management.core.exceptions; + +public class GlusterRuntimeException extends RuntimeException { + + public GlusterRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public GlusterRuntimeException(String message) { + super(message); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/AuthStatus.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/AuthStatus.java new file mode 100644 index 00000000..b95fa804 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/AuthStatus.java @@ -0,0 +1,17 @@ +package com.gluster.storage.management.core.model; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class AuthStatus { + private boolean isAuthenticated; + + public boolean getIsAuthenticated() { + return isAuthenticated; + } + + public void setIsAuthenticated(boolean authenticated) { + this.isAuthenticated = authenticated; + } + +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Cluster.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Cluster.java new file mode 100644 index 00000000..5ae07fd4 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Cluster.java @@ -0,0 +1,52 @@ +package com.gluster.storage.management.core.model; + +import java.util.ArrayList; +import java.util.List; + +public class Cluster extends Entity { + private List<IClusterListener> listeners = new ArrayList<IClusterListener>(); + List<GlusterServer> servers = new ArrayList<GlusterServer>(); + List<Server> autoDiscoveredServers = new ArrayList<Server>(); + List<Volume> volumes = new ArrayList<Volume>(); + + public List<GlusterServer> getServers() { + return servers; + } + + public void setServers(List<GlusterServer> servers) { + this.servers = servers; + children.add(new EntityGroup<GlusterServer>("Servers", GlusterServer.class, this, servers)); + } + + public List<Server> getAutoDiscoveredServers() { + return autoDiscoveredServers; + } + + public void setAutoDiscoveredServers(List<Server> autoDiscoveredServers) { + this.autoDiscoveredServers = autoDiscoveredServers; + children.add(new EntityGroup<Server>("Discovered Servers", Server.class, this, autoDiscoveredServers)); + } + + public List<Volume> getVolumes() { + return volumes; + } + + public void setVolumes(List<Volume> volumes) { + this.volumes = volumes; + children.add(new EntityGroup<Volume>("Volumes", Volume.class, this, volumes)); + } + + public Cluster(String name, Entity parent) { + super(name, parent); + } + + public Cluster(String name, Entity parent, List<GlusterServer> servers, List<Volume> volumes) { + super(name, parent); + setServers(servers); + setVolumes(volumes); + } + + public void addClusterListener(IClusterListener listener) { + listeners.add(listener); + } +}
\ No newline at end of file diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/ConnectionDetails.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/ConnectionDetails.java new file mode 100644 index 00000000..5d7e2d09 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/ConnectionDetails.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2004, 2005 Jean-Michel Lemieux, Jeff McAffer and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Hyperbola is an RCP application developed for the book + * Eclipse Rich Client Platform - + * Designing, Coding, and Packaging Java Applications + * + * Contributors: + * Jean-Michel Lemieux and Jeff McAffer - initial implementation + *******************************************************************************/ +package com.gluster.storage.management.core.model; + +public class ConnectionDetails { + private String userId, server, password; + + public ConnectionDetails() { + + } + + public ConnectionDetails(String server, String userId, String password) { + this.userId = userId; + this.server = server; + this.password = password; + } + + public String getUserId() { + return userId; + } + + public String getServer() { + return server; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/DefaultClusterListener.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/DefaultClusterListener.java new file mode 100644 index 00000000..66d4e891 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/DefaultClusterListener.java @@ -0,0 +1,32 @@ +package com.gluster.storage.management.core.model; + +/** + * Default listener - doesn't do anything. Sub-class and override the method for + * the event you want to handle. + */ +public class DefaultClusterListener implements IClusterListener { + + @Override + public void serverAdded(GlusterServer server) { + } + + @Override + public void serverRemoved(GlusterServer server) { + } + + @Override + public void serverChanged(GlusterServer server, Event event) { + } + + @Override + public void volumeAdded(Volume volume) { + } + + @Override + public void volumeRemoved(Volume volume) { + } + + @Override + public void volumeChanged(Volume volume, Event event) { + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Disk.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Disk.java new file mode 100644 index 00000000..b5093627 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Disk.java @@ -0,0 +1,81 @@ +package com.gluster.storage.management.core.model; + +import com.gluster.storage.management.core.utils.StringUtils; + +public class Disk extends Entity { + public enum DISK_STATUS { + READY, UNINITIALIZED, INITIALIZING, OFFLINE + }; + + private String[] DISK_STATUS_STR = { "Ready", "Uninitialized", "Initializing", "Offline" }; + + private Server server; + private Double space; + private Double spaceInUse; + private DISK_STATUS status; + + public Double getSpace() { + return space; + } + + public void setSpace(Double space) { + this.space = space; + } + + public boolean isUninitialized() { + return getStatus() == DISK_STATUS.UNINITIALIZED; + } + + public boolean isOffline() { + return getStatus() == DISK_STATUS.OFFLINE; + } + + public boolean isReady() { + return getStatus() == DISK_STATUS.READY; + } + + public DISK_STATUS getStatus() { + return status; + } + + public String getStatusStr() { + return DISK_STATUS_STR[getStatus().ordinal()]; + } + + public void setStatus(DISK_STATUS status) { + this.status = status; + } + + public Double getSpaceInUse() { + return spaceInUse; + } + + public void setSpaceInUse(Double spaceInUse) { + this.spaceInUse = spaceInUse; + } + + public Server getServer() { + return server; + } + + public void setServer(Server server) { + this.server = server; + } + + public Disk(Server server, String name, Double space, Double spaceInUse, DISK_STATUS status) { + super(name, server); + setServer(server); + setSpace(space); + setSpaceInUse(spaceInUse); + setStatus(status); + } + + @Override + public boolean filter(String filterString, boolean caseSensitive) { + return StringUtils.filterString(getServer().getName() + getName() + getStatusStr(), filterString, caseSensitive); + } + + public String getQualifiedName() { + return getServer().getName() + ":" + getName(); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Entity.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Entity.java new file mode 100644 index 00000000..8c56a035 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Entity.java @@ -0,0 +1,56 @@ +package com.gluster.storage.management.core.model; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.PlatformObject; + +import com.gluster.storage.management.core.utils.StringUtils; + +public class Entity extends PlatformObject implements Filterable { + private static final long serialVersionUID = 1L; + + protected String name; + protected List<Entity> children = new ArrayList<Entity>(); + private Entity parent; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Entity getParent() { + return parent; + } + + public void setParent(Entity parent) { + this.parent = parent; + } + + public List<? extends Entity> getChildren() { + return children; + } + + public void setChildren(List<Entity> children) { + this.children = children; + } + + public Entity(String name, Entity parent) { + this.name = name; + this.parent = parent; + } + + public Entity(String name, Entity parent, List<Entity> children) { + this.name = name; + this.parent = parent; + this.children = children; + } + + @Override + public boolean filter(String filterString, boolean caseSensitive) { + return StringUtils.filterString(getName(), filterString, caseSensitive); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/EntityGroup.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/EntityGroup.java new file mode 100644 index 00000000..18b72488 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/EntityGroup.java @@ -0,0 +1,28 @@ +package com.gluster.storage.management.core.model; + +import java.util.List; + +public class EntityGroup<T> extends Entity { + private Class<? extends Entity> type; + + public EntityGroup(String name, Class<? extends Entity> type, Cluster cluster) { + this(name, type, cluster, null); + } + + public EntityGroup(String name, Class<? extends Entity> type, Cluster cluster, List<T> entities) { + super(name, cluster, (List<Entity>)entities); + this.type = type; + } + + public List<? extends Entity> getEntities() { + return children; + } + + public void setEntities(List<T> entities) { + children = (List<Entity>)entities; + } + + public Class<? extends Entity> getEntityType() { + return type; + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Event.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Event.java new file mode 100644 index 00000000..5b0a0019 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Event.java @@ -0,0 +1,18 @@ +package com.gluster.storage.management.core.model; + +public class Event { + public enum EVENT_TYPE { + DISK_ADDED, + DISK_REMOVED, + NETWORK_INTERFACE_ADDED, + NETWORK_INTERFACE_REMOVED + } + + private EVENT_TYPE eventType; + private Object eventData; + + public Event(EVENT_TYPE eventType, Object eventData) { + this.eventType = eventType; + this.eventData = eventData; + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Filterable.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Filterable.java new file mode 100644 index 00000000..52b61b6b --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Filterable.java @@ -0,0 +1,19 @@ +package com.gluster.storage.management.core.model; + +/** + * By default, the {@link EntityViewerFilter} filters the objects by parsing the + * output of {@link Object#toString()} with the filter string. Classes that need + * specific filtering logic can implement this interface. The default logic will + * then be overridden by the method {@link Filterable#filter(String)}. + */ +public interface Filterable { + /** + * @param filterString + * String to be used for filtering + * @param caseSensitive + * Flag indicating whether the filtering should be case sensitive + * @return true if the object can be selected using the filter string, else + * false + */ + public boolean filter(String filterString, boolean caseSensitive); +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterDataModel.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterDataModel.java new file mode 100644 index 00000000..4256bd9c --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterDataModel.java @@ -0,0 +1,24 @@ +package com.gluster.storage.management.core.model; + +import java.util.ArrayList; +import java.util.List; + +public class GlusterDataModel extends Entity { + public GlusterDataModel(String name, List<Cluster> clusters) { + super(name, null); + children.addAll(clusters); + } + + public GlusterDataModel(String name) { + this(name, new ArrayList<Cluster>()); + } + + public void setClusters(List<Cluster> clusters) { + children.clear(); + children.addAll(clusters); + } + + public void addCluster(Cluster cluster) { + children.add(cluster); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterDummyModel.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterDummyModel.java new file mode 100644 index 00000000..23b25ee5 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterDummyModel.java @@ -0,0 +1,264 @@ +package com.gluster.storage.management.core.model; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import com.gluster.storage.management.core.model.Disk.DISK_STATUS; +import com.gluster.storage.management.core.model.GlusterServer.SERVER_STATUS; +import com.gluster.storage.management.core.model.Volume.TRANSPORT_TYPE; +import com.gluster.storage.management.core.model.Volume.VOLUME_STATUS; +import com.gluster.storage.management.core.model.Volume.VOLUME_TYPE; + +public class GlusterDummyModel { + // private Server discoveredServer1, discoveredServer2, discoveredServer3, discoveredServer4, discoveredServer5; + private GlusterServer server1, server2, server3, server4, server5; + private Volume volume1, volume2, volume3, volume4, volume5; + private Disk s1da, s1db, s2da, s2db, s2dc, s2dd, s3da, s4da, s5da, s5db; + private static List<LogMessage> logMessages = new ArrayList<LogMessage>(); + private static GlusterDummyModel instance = new GlusterDummyModel(); + private GlusterDataModel model; + + private GlusterDummyModel() { + model = initializeModel(); + } + + public GlusterDataModel getModel() { + return model; + } + + public static GlusterDummyModel getInstance() { + return instance; + } + + private GlusterServer addGlusterServer(List<GlusterServer> servers, Entity parent, String name, + SERVER_STATUS status, String preferredInterfaceName, int numOfCPUs, double cpuUsage, double totalMemory, + double memoryInUse) { + GlusterServer glusterServer = new GlusterServer(name, parent, status, numOfCPUs, cpuUsage, totalMemory, + memoryInUse); + NetworkInterface networkInterface = addNetworkInterface(glusterServer, preferredInterfaceName); + glusterServer.setPreferredNetworkInterface(networkInterface); + + servers.add(glusterServer); + return glusterServer; + } + + private NetworkInterface addNetworkInterface(Server server, String interfaceName) { + NetworkInterface networkInterface = new NetworkInterface(interfaceName, server, "192.168.1." + + Math.round(Math.random() * 255), "255.255.255.0", "192.168.1.1"); + server.setNetworkInterfaces(Arrays.asList(new NetworkInterface[] { networkInterface })); + return networkInterface; + } + + private void addDiscoveredServer(List<Server> servers, Entity parent, String name, int numOfCPUs, double cpuUsage, + double totalMemory, double memoryInUse, double totalDiskSpace, double diskSpaceInUse) { + Server server = new Server(name, parent, numOfCPUs, cpuUsage, totalMemory, memoryInUse); + server.addDisk(new Disk(server, "sda", totalDiskSpace, diskSpaceInUse, DISK_STATUS.READY)); + addNetworkInterface(server, "eth0"); + + servers.add(server); + } + + private GlusterDataModel initializeModel() { + // Create the dummy data model for demo + GlusterDataModel model = new GlusterDataModel("Clusters"); + Cluster cluster = new Cluster("Home", model); + + initializeGlusterServers(cluster); + initializeVolumes(cluster); + initializeAutoDiscoveredServers(cluster); + initializeDisks(); + addDisksToServers(); + addDisksToVolumes(); + addVolumeOptions(); + + createDummyLogMessages(); + + model.addCluster(cluster); + return model; + } + + private void addVolumeOptions() { + for (Volume vol : new Volume[] { volume1, volume2, volume3, volume4, volume5 }) { + for (int i = 1; i <= 5; i++) { + String key = vol.getName() + "key" + i; + String value = vol.getName() + "value" + i; + vol.setOption(key, value); + } + } + } + + private Volume addVolume(List<Volume> volumes, String name, Cluster cluster, VOLUME_TYPE volumeType, + TRANSPORT_TYPE transportType, VOLUME_STATUS status) { + Volume volume = new Volume(name, cluster, volumeType, transportType, status); + volumes.add(volume); + + return volume; + } + + private void initializeVolumes(Cluster cluster) { + List<Volume> volumes = new ArrayList<Volume>(); + + volume1 = addVolume(volumes, "Volume1", cluster, VOLUME_TYPE.PLAIN_DISTRIBUTE, TRANSPORT_TYPE.ETHERNET, + VOLUME_STATUS.ONLINE); + + volume2 = addVolume(volumes, "Volume2", cluster, VOLUME_TYPE.PLAIN_DISTRIBUTE, TRANSPORT_TYPE.ETHERNET, + VOLUME_STATUS.ONLINE); + + volume3 = addVolume(volumes, "Volume3", cluster, VOLUME_TYPE.DISTRIBUTED_MIRROR, TRANSPORT_TYPE.ETHERNET, + VOLUME_STATUS.OFFLINE); + volume3.setReplicaCount(2); + + volume4 = addVolume(volumes, "Volume4", cluster, VOLUME_TYPE.PLAIN_DISTRIBUTE, TRANSPORT_TYPE.ETHERNET, + VOLUME_STATUS.ONLINE); + + volume5 = addVolume(volumes, "Volume5", cluster, VOLUME_TYPE.DISTRIBUTED_STRIPE, TRANSPORT_TYPE.INFINIBAND, + VOLUME_STATUS.OFFLINE); + volume5.setStripeCount(3); + + cluster.setVolumes(volumes); + } + + private void initializeDisks() { + s1da = new Disk(server1, "sda", 100d, 80d, DISK_STATUS.READY); + s1db = new Disk(server1, "sdb", 100d, 67.83, DISK_STATUS.READY); + + s2da = new Disk(server2, "sda", 200d, 157.12, DISK_STATUS.READY); + s2db = new Disk(server2, "sdb", 200d, 182.27, DISK_STATUS.READY); + s2dc = new Disk(server2, "sdc", 200d, -1d, DISK_STATUS.UNINITIALIZED); + s2dd = new Disk(server2, "sdd", 200d, 124.89, DISK_STATUS.READY); + + s3da = new Disk(server3, "NA", -1d, -1d, DISK_STATUS.OFFLINE); // disk name unavailable since server is offline + + s4da = new Disk(server4, "sda", 100d, 85.39, DISK_STATUS.READY); + + s5da = new Disk(server5, "sda", 100d, 92.83, DISK_STATUS.READY); + s5db = new Disk(server5, "sdb", 200d, 185.69, DISK_STATUS.READY); + } + + private void addDisksToServers() { + server1.addDisk(s1da); + server1.addDisk(s1db); + + server2.addDisk(s2da); + server2.addDisk(s2db); + server2.addDisk(s2dc); + server2.addDisk(s2dd); + + // server3.addDisk(s3da); + + server4.addDisk(s4da); + + server5.addDisk(s5da); + server5.addDisk(s5db); + } + + private void addDisksToVolumes() { + volume1.addDisk(s1da); + + volume2.addDisk(s2da); + volume2.addDisk(s1db); + volume2.addDisk(s3da); + volume2.addDisk(s4da); + + volume3.addDisk(s2db); + volume3.addDisk(s4da); + volume3.addDisk(s5da); + + volume4.addDisk(s1da); + volume4.addDisk(s3da); + volume4.addDisk(s4da); + volume4.addDisk(s5db); + + volume5.addDisk(s2da); + volume5.addDisk(s5db); + } + + private void initializeGlusterServers(Cluster cluster) { + List<GlusterServer> servers = new ArrayList<GlusterServer>(); + server1 = addGlusterServer(servers, cluster, "Server1", SERVER_STATUS.ONLINE, "eth0", 4, 56.3, 16, 8.4); + server2 = addGlusterServer(servers, cluster, "Server2", SERVER_STATUS.ONLINE, "eth1", 8, 41.92, 32, 18.76); + server3 = addGlusterServer(servers, cluster, "Server3", SERVER_STATUS.OFFLINE, "eth0", -1, -1, -1, -1); + server4 = addGlusterServer(servers, cluster, "Server4", SERVER_STATUS.ONLINE, "eth0", 1, 92.83, 4, 3.18); + server5 = addGlusterServer(servers, cluster, "Server5", SERVER_STATUS.ONLINE, "inf0", 2, 87.24, 8, 7.23); + + cluster.setServers(servers); + } + + private void initializeAutoDiscoveredServers(Cluster cluster) { + List<Server> servers = new ArrayList<Server>(); + addDiscoveredServer(servers, cluster, "ADServer1", 4, 56.3, 16, 8.4, 200, 147.83); + addDiscoveredServer(servers, cluster, "ADServer2", 8, 41.92, 32, 18.76, 800, 464.28); + addDiscoveredServer(servers, cluster, "ADServer3", 2, 84.28, 2, 1.41, 120, 69.93); + addDiscoveredServer(servers, cluster, "ADServer4", 1, 92.83, 4, 3.18, 100, 85.39); + addDiscoveredServer(servers, cluster, "ADServer5", 2, 87.24, 8, 7.23, 250, 238.52); + cluster.setAutoDiscoveredServers(servers); + } + + private void addMessages(List<LogMessage> messages, Disk disk, String severity, int count) { + for (int i = 1; i <= count; i++) { + String message = severity + "message" + i; + messages.add(new LogMessage(new Date(), disk, severity, message)); + } + } + + private void addMessagesForDisk(List<LogMessage> logMessages, Disk disk) { + addMessages(logMessages, disk, "SEVERE", 5); + addMessages(logMessages, disk, "WARNING", 5); + addMessages(logMessages, disk, "DEBUG", 5); + addMessages(logMessages, disk, "INFO", 5); + } + + public List<LogMessage> createDummyLogMessages() { + addMessagesForDisk(logMessages, s1da); + addMessagesForDisk(logMessages, s1db); + addMessagesForDisk(logMessages, s2da); + addMessagesForDisk(logMessages, s2db); + addMessagesForDisk(logMessages, s2dc); + addMessagesForDisk(logMessages, s2dd); + addMessagesForDisk(logMessages, s4da); + addMessagesForDisk(logMessages, s5da); + addMessagesForDisk(logMessages, s5db); + return logMessages; + } + + public static List<LogMessage> getDummyLogMessages() { + return logMessages; + } + + public List<Disk> getReadyDisksOfVolume(Volume volume) { + List<Disk> disks = new ArrayList<Disk>(); + for (Disk disk : volume.getDisks()) { + if (disk.isReady()) { + disks.add(disk); + } + } + return disks; + } + + public List<Disk> getReadyDisksOfAllVolumes() { + List<Disk> disks = new ArrayList<Disk>(); + for (Volume volume : ((Cluster) model.getChildren().get(0)).getVolumes()) { + disks.addAll(getReadyDisksOfVolume(volume)); + } + return disks; + } + + public List<Disk> getReadyDisksOfAllServers() { + return getReadyDisksOfAllServersExcluding(new ArrayList<Disk>()); + } + + public List<Disk> getReadyDisksOfAllServersExcluding(List<Disk> excludeDisks) { + List<Disk> disks = new ArrayList<Disk>(); + + for (Server server : ((Cluster) model.getChildren().get(0)).getServers()) { + for (Disk disk : server.getDisks()) { + if (disk.isReady() && !excludeDisks.contains(disk)) { + disks.add(disk); + } + } + } + return disks; + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterServer.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterServer.java new file mode 100644 index 00000000..ee065032 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/GlusterServer.java @@ -0,0 +1,65 @@ +package com.gluster.storage.management.core.model; + +import com.gluster.storage.management.core.utils.StringUtils; + +public class GlusterServer extends Server { + public enum SERVER_STATUS { + ONLINE, OFFLINE + }; + private static final String[] STATUS_STR = new String[] { "Online", "Offline" }; + + private SERVER_STATUS status; + private NetworkInterface preferredNetworkInterface; + private Cluster cluster; + + public GlusterServer(String name, Entity parent, SERVER_STATUS status, int numOfCPUs, double cpuUsage, double totalMemory, + double memoryInUse) { + super(name, parent, numOfCPUs, cpuUsage, totalMemory, memoryInUse); + setStatus(status); + } + + public GlusterServer(String name, Entity parent, SERVER_STATUS status, int numOfCPUs, double cpuUsage, double totalMemory, + double memoryInUse, Cluster cluster) { + this(name, parent, status, numOfCPUs, cpuUsage, totalMemory, memoryInUse); + setCluster(cluster); + } + + public String getStatusStr() { + return STATUS_STR[getStatus().ordinal()]; + } + + public SERVER_STATUS getStatus() { + return status; + } + + public void setStatus(SERVER_STATUS status) { + this.status = status; + } + + public NetworkInterface getPreferredNetworkInterface() { + return preferredNetworkInterface; + } + + public void setPreferredNetworkInterface(NetworkInterface preferredNetworkInterface) { + this.preferredNetworkInterface = preferredNetworkInterface; + preferredNetworkInterface.setPreferred(true); + } + + public Cluster getCluster() { + return cluster; + } + + public void setCluster(Cluster cluster) { + this.cluster = cluster; + } + + /** + * Filter matches if any of the properties name, status, preferred network interface, and primary/secondary/third + * DNS contains the filter string + */ + @Override + public boolean filter(String filterString, boolean caseSensitive) { + return StringUtils.filterString(getName() + getStatusStr() + getPreferredNetworkInterface().getName(), + filterString, caseSensitive); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/IClusterListener.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/IClusterListener.java new file mode 100644 index 00000000..32454b59 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/IClusterListener.java @@ -0,0 +1,19 @@ +package com.gluster.storage.management.core.model; + +/** + * Interface for a cluster listener. Every registered listener will be notified + * on various events happening on the cluster. + */ +public interface IClusterListener { + public void serverAdded(GlusterServer server); + + public void serverRemoved(GlusterServer server); + + public void serverChanged(GlusterServer server, Event event); + + public void volumeAdded(Volume volume); + + public void volumeRemoved(Volume volume); + + public void volumeChanged(Volume volume, Event event); +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/LogMessage.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/LogMessage.java new file mode 100644 index 00000000..fc324b03 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/LogMessage.java @@ -0,0 +1,57 @@ +package com.gluster.storage.management.core.model; + +import java.util.Date; + +import com.gluster.storage.management.core.utils.StringUtils; + +public class LogMessage implements Filterable { + private Date timestamp; + private Disk disk; + private String severity; + private String message; + + public Date getTimestamp() { + return timestamp; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + public Disk getDisk() { + return disk; + } + + public void setDisk(Disk disk) { + this.disk = disk; + } + + public String getSeverity() { + return severity; + } + + public void setSeverity(String severity) { + this.severity = severity; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public LogMessage(Date timestamp, Disk disk, String severity, String message) { + setTimestamp(timestamp); + setDisk(disk); + setSeverity(severity); + setMessage(message); + } + + @Override + public boolean filter(String filterString, boolean caseSensitive) { + return StringUtils.filterString(getSeverity() + getTimestamp() + getDisk().getServer().getName() + + getDisk().getQualifiedName() + getMessage(), filterString, caseSensitive); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/NetworkInterface.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/NetworkInterface.java new file mode 100644 index 00000000..375b1971 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/NetworkInterface.java @@ -0,0 +1,48 @@ +package com.gluster.storage.management.core.model; + +public class NetworkInterface extends Entity { + private String ipAddress; + private String netMask; + private String defaultGateway; + private boolean isPreferred; + + public boolean isPreferred() { + return isPreferred; + } + + public void setPreferred(boolean isPreferred) { + this.isPreferred = isPreferred; + } + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + + public String getNetMask() { + return netMask; + } + + public void setNetMask(String netMask) { + this.netMask = netMask; + } + + public String getDefaultGateway() { + return defaultGateway; + } + + public void setDefaultGateway(String defaultGateway) { + this.defaultGateway = defaultGateway; + } + + public NetworkInterface(String name, Entity parent, String ipAddress, String netMask, String defaultGateway) { + super(name, parent); + setIpAddress(ipAddress); + setNetMask(netMask); + setDefaultGateway(defaultGateway); + } + +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java new file mode 100644 index 00000000..b7e56ba4 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Server.java @@ -0,0 +1,126 @@ +package com.gluster.storage.management.core.model; + +import java.util.ArrayList; +import java.util.List; + +import com.gluster.storage.management.core.utils.StringUtils; + +public class Server extends Entity { + private List<NetworkInterface> networkInterfaces = new ArrayList<NetworkInterface>(); + private int numOfCPUs; + private double cpuUsage; + private double totalMemory; + private double memoryInUse; + private double totalDiskSpace = 0; + private double diskSpaceInUse = 0; + private List<Disk> disks = new ArrayList<Disk>(); + + public Server(String name, Entity parent, int numOfCPUs, double cpuUsage, double totalMemory, double memoryInUse) { + super(name, parent); + setNumOfCPUs(numOfCPUs); + setCpuUsage(cpuUsage); + setTotalMemory(totalMemory); + setMemoryInUse(memoryInUse); + } + + public int getNumOfCPUs() { + return numOfCPUs; + } + + public void setNumOfCPUs(int numOfCPUs) { + this.numOfCPUs = numOfCPUs; + } + + public double getCpuUsage() { + return cpuUsage; + } + + public void setCpuUsage(double cpuUsage) { + this.cpuUsage = cpuUsage; + } + + public double getTotalMemory() { + return totalMemory; + } + + public void setTotalMemory(double totalMemory) { + this.totalMemory = totalMemory; + } + + public double getMemoryInUse() { + return memoryInUse; + } + + public void setMemoryInUse(double memoryInUse) { + this.memoryInUse = memoryInUse; + } + + public double getTotalDiskSpace() { + return totalDiskSpace; + } + + public double getDiskSpaceInUse() { + return diskSpaceInUse; + } + + public List<NetworkInterface> getNetworkInterfaces() { + return networkInterfaces; + } + + public void setNetworkInterfaces(List<NetworkInterface> networkInterfaces) { + this.networkInterfaces = networkInterfaces; + } + + public List<Disk> getDisks() { + return disks; + } + + public void addDisk(Disk disk) { + if (disks.add(disk)) { + totalDiskSpace += disk.getSpace(); + diskSpaceInUse += disk.getSpaceInUse(); + } + } + + public void addDisks(List<Disk> disks) { + for(Disk disk : disks) { + addDisk(disk); + } + } + + public void removeDisk(Disk disk) { + if (disks.remove(disk)) { + totalDiskSpace -= disk.getSpace(); + diskSpaceInUse -= disk.getSpaceInUse(); + } + } + + public void removeAllDisks() { + disks.clear(); + totalDiskSpace = 0; + diskSpaceInUse = 0; + } + + public void setDisks(List<Disk> disks) { + removeAllDisks(); + addDisks(disks); + } + + public int getNumOfDisks() { + return disks.size(); + } + + public String getIpAddressesAsString() { + String ipAddresses = ""; + for (NetworkInterface networkInterface : getNetworkInterfaces()) { + String ipAddr = networkInterface.getIpAddress(); + ipAddresses += (ipAddresses.isEmpty() ? ipAddr : ", " + ipAddr); + } + return ipAddresses; + } + + @Override + public boolean filter(String filterString, boolean caseSensitive) { + return StringUtils.filterString(getName() + getIpAddressesAsString(), filterString, caseSensitive); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/User.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/User.java new file mode 100644 index 00000000..3043126d --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/User.java @@ -0,0 +1,22 @@ +package com.gluster.storage.management.core.model; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class User { + private String userId; + private String password; + + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Volume.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Volume.java new file mode 100644 index 00000000..2dd7bc3d --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/model/Volume.java @@ -0,0 +1,228 @@ +package com.gluster.storage.management.core.model; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.bind.annotation.XmlRootElement; + +import com.gluster.storage.management.core.model.Disk.DISK_STATUS; +import com.gluster.storage.management.core.utils.StringUtils; + +public class Volume extends Entity { + public enum VOLUME_STATUS { + ONLINE, OFFLINE + }; + + public enum VOLUME_TYPE { + PLAIN_DISTRIBUTE, DISTRIBUTED_MIRROR, DISTRIBUTED_STRIPE + }; + + public enum TRANSPORT_TYPE { + ETHERNET, INFINIBAND + }; + + public enum NAS_PROTOCOL { + GLUSTERFS, NFS + }; + + private static final String[] VOLUME_TYPE_STR = new String[] { "Plain Distribute", "Distributed Mirror", + "Distributed Stripe" }; + private static final String[] TRANSPORT_TYPE_STR = new String[] { "Ethernet", "Infiniband" }; + private static final String[] STATUS_STR = new String[] { "Online", "Offline" }; + private static final String[] NAS_PROTOCOL_STR = new String[] { "Gluster", "NFS" }; + + private Cluster cluster; + private VOLUME_TYPE volumeType; + private TRANSPORT_TYPE transportType; + private VOLUME_STATUS status; + private int replicaCount; + private int stripeCount; + private Map<String, String> options = new LinkedHashMap<String, String>(); + + private double totalDiskSpace = 0; + private List<Disk> disks = new ArrayList<Disk>(); + + // GlusterFS export is always enabled + private Set<NAS_PROTOCOL> nasProtocols = new LinkedHashSet<NAS_PROTOCOL>( + Arrays.asList(new NAS_PROTOCOL[] { NAS_PROTOCOL.GLUSTERFS })); + + private String accessControlList = "*"; + + public String getVolumeTypeStr() { + return getVolumeTypeStr(getVolumeType()); + } + + public static String getVolumeTypeStr(VOLUME_TYPE volumeType) { + return VOLUME_TYPE_STR[volumeType.ordinal()]; + } + + public String getTransportTypeStr() { + return TRANSPORT_TYPE_STR[getTransportType().ordinal()]; + } + + public String getStatusStr() { + return STATUS_STR[getStatus().ordinal()]; + } + + public int getNumOfDisks() { + return disks.size(); + } + + public VOLUME_TYPE getVolumeType() { + return volumeType; + } + + public void setVolumeType(VOLUME_TYPE volumeType) { + this.volumeType = volumeType; + } + + public TRANSPORT_TYPE getTransportType() { + return transportType; + } + + public void setTransportType(TRANSPORT_TYPE transportType) { + this.transportType = transportType; + } + + public VOLUME_STATUS getStatus() { + return status; + } + + public int getReplicaCount() { + return replicaCount; + } + + public void setReplicaCount(int replicaCount) { + this.replicaCount = replicaCount; + } + + public int getStripeCount() { + return stripeCount; + } + + public void setStripeCount(int stripeCount) { + this.stripeCount = stripeCount; + } + + public void setStatus(VOLUME_STATUS status) { + this.status = status; + } + + public Cluster getCluster() { + return cluster; + } + + public void setCluster(Cluster cluster) { + this.cluster = cluster; + } + + public Set<NAS_PROTOCOL> getNASProtocols() { + return nasProtocols; + } + + public void setNASProtocols(Set<NAS_PROTOCOL> nasProtocols) { + this.nasProtocols = nasProtocols; + } + + public String getNASProtocolsStr() { + String protocolsStr = ""; + for (NAS_PROTOCOL protocol : nasProtocols) { + String protocolStr = NAS_PROTOCOL_STR[protocol.ordinal()]; + protocolsStr += (protocolsStr.isEmpty() ? protocolStr : ", " + protocolStr); + } + return protocolsStr; + } + + public String getAccessControlList() { + return accessControlList; + } + + public void setAccessControlList(String accessControlList) { + this.accessControlList = accessControlList; + } + + public Map<String, String> getOptions() { + return options; + } + + public void setOption(String key, String value) { + options.put(key, value); + } + + public void setOptions(Map<String, String> options) { + this.options = options; + } + + public double getTotalDiskSpace() { + return totalDiskSpace; + } + + public List<Disk> getDisks() { + return disks; + } + + public void addDisk(Disk disk) { + if (disks.add(disk) && disk.getStatus() != DISK_STATUS.OFFLINE) { + totalDiskSpace += disk.getSpace(); + } + } + + public void addDisks(List<Disk> disks) { + for (Disk disk : disks) { + addDisk(disk); + } + } + + public void removeDisk(Disk disk) { + if (disks.remove(disk)) { + totalDiskSpace -= disk.getSpace(); + } + } + + public void removeAllDisks() { + disks.clear(); + totalDiskSpace = 0; + } + + public void setDisks(List<Disk> disks) { + removeAllDisks(); + addDisks(disks); + } + + public void enableNFS() { + nasProtocols.add(NAS_PROTOCOL.NFS); + } + + public void disableNFS() { + nasProtocols.remove(NAS_PROTOCOL.NFS); + } + + public Volume(String name, Entity parent, VOLUME_TYPE volumeType, TRANSPORT_TYPE transportType, VOLUME_STATUS status) { + super(name, parent); + setVolumeType(volumeType); + setTransportType(transportType); + setStatus(status); + } + + public Volume(String name, Entity parent, Cluster cluster, VOLUME_TYPE volumeType, TRANSPORT_TYPE transportType, + VOLUME_STATUS status) { + this(name, parent, volumeType, transportType, status); + + setCluster(cluster); + } + + /** + * Filter matches if any of the properties name, volume type, transport type, status and number of disks contains + * the filter string + */ + @Override + public boolean filter(String filterString, boolean caseSensitive) { + return StringUtils.filterString(getName() + getVolumeTypeStr() + getTransportTypeStr() + getStatusStr() + + getNumOfDisks(), filterString, caseSensitive); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/Crypt.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/Crypt.java new file mode 100644 index 00000000..adafd477 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/Crypt.java @@ -0,0 +1,560 @@ +/**************************************************************************** + * Java-based implementation of the unix crypt(3) command + * + * Based upon C source code written by Eric Young, eay@psych.uq.oz.au + * Java conversion by John F. Dumas, jdumas@zgs.com + * + * Found at http://locutus.kingwoodcable.com/jfd/crypt.html + * Minor optimizations by Wes Biggs, wes@cacas.org + * + * Eric's original code is licensed under the BSD license. As this is + * derivative, the same license applies. + * + * Note: Crypt.class is much smaller when compiled with javac -O + ****************************************************************************/ +package com.gluster.storage.management.core.utils; + +public class Crypt { + private Crypt() {} // defined so class can't be instantiated. + + private static final int ITERATIONS = 16; + + private static final boolean shifts2[] = { + false, false, true, true, true, true, true, true, + false, true, true, true, true, true, true, false + }; + + private static final int skb[][] = { + { + /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ + 0x00000000, 0x00000010, 0x20000000, 0x20000010, + 0x00010000, 0x00010010, 0x20010000, 0x20010010, + 0x00000800, 0x00000810, 0x20000800, 0x20000810, + 0x00010800, 0x00010810, 0x20010800, 0x20010810, + 0x00000020, 0x00000030, 0x20000020, 0x20000030, + 0x00010020, 0x00010030, 0x20010020, 0x20010030, + 0x00000820, 0x00000830, 0x20000820, 0x20000830, + 0x00010820, 0x00010830, 0x20010820, 0x20010830, + 0x00080000, 0x00080010, 0x20080000, 0x20080010, + 0x00090000, 0x00090010, 0x20090000, 0x20090010, + 0x00080800, 0x00080810, 0x20080800, 0x20080810, + 0x00090800, 0x00090810, 0x20090800, 0x20090810, + 0x00080020, 0x00080030, 0x20080020, 0x20080030, + 0x00090020, 0x00090030, 0x20090020, 0x20090030, + 0x00080820, 0x00080830, 0x20080820, 0x20080830, + 0x00090820, 0x00090830, 0x20090820, 0x20090830, + }, + { + /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ + 0x00000000, 0x02000000, 0x00002000, 0x02002000, + 0x00200000, 0x02200000, 0x00202000, 0x02202000, + 0x00000004, 0x02000004, 0x00002004, 0x02002004, + 0x00200004, 0x02200004, 0x00202004, 0x02202004, + 0x00000400, 0x02000400, 0x00002400, 0x02002400, + 0x00200400, 0x02200400, 0x00202400, 0x02202400, + 0x00000404, 0x02000404, 0x00002404, 0x02002404, + 0x00200404, 0x02200404, 0x00202404, 0x02202404, + 0x10000000, 0x12000000, 0x10002000, 0x12002000, + 0x10200000, 0x12200000, 0x10202000, 0x12202000, + 0x10000004, 0x12000004, 0x10002004, 0x12002004, + 0x10200004, 0x12200004, 0x10202004, 0x12202004, + 0x10000400, 0x12000400, 0x10002400, 0x12002400, + 0x10200400, 0x12200400, 0x10202400, 0x12202400, + 0x10000404, 0x12000404, 0x10002404, 0x12002404, + 0x10200404, 0x12200404, 0x10202404, 0x12202404, + }, + { + /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ + 0x00000000, 0x00000001, 0x00040000, 0x00040001, + 0x01000000, 0x01000001, 0x01040000, 0x01040001, + 0x00000002, 0x00000003, 0x00040002, 0x00040003, + 0x01000002, 0x01000003, 0x01040002, 0x01040003, + 0x00000200, 0x00000201, 0x00040200, 0x00040201, + 0x01000200, 0x01000201, 0x01040200, 0x01040201, + 0x00000202, 0x00000203, 0x00040202, 0x00040203, + 0x01000202, 0x01000203, 0x01040202, 0x01040203, + 0x08000000, 0x08000001, 0x08040000, 0x08040001, + 0x09000000, 0x09000001, 0x09040000, 0x09040001, + 0x08000002, 0x08000003, 0x08040002, 0x08040003, + 0x09000002, 0x09000003, 0x09040002, 0x09040003, + 0x08000200, 0x08000201, 0x08040200, 0x08040201, + 0x09000200, 0x09000201, 0x09040200, 0x09040201, + 0x08000202, 0x08000203, 0x08040202, 0x08040203, + 0x09000202, 0x09000203, 0x09040202, 0x09040203, + }, + { + /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ + 0x00000000, 0x00100000, 0x00000100, 0x00100100, + 0x00000008, 0x00100008, 0x00000108, 0x00100108, + 0x00001000, 0x00101000, 0x00001100, 0x00101100, + 0x00001008, 0x00101008, 0x00001108, 0x00101108, + 0x04000000, 0x04100000, 0x04000100, 0x04100100, + 0x04000008, 0x04100008, 0x04000108, 0x04100108, + 0x04001000, 0x04101000, 0x04001100, 0x04101100, + 0x04001008, 0x04101008, 0x04001108, 0x04101108, + 0x00020000, 0x00120000, 0x00020100, 0x00120100, + 0x00020008, 0x00120008, 0x00020108, 0x00120108, + 0x00021000, 0x00121000, 0x00021100, 0x00121100, + 0x00021008, 0x00121008, 0x00021108, 0x00121108, + 0x04020000, 0x04120000, 0x04020100, 0x04120100, + 0x04020008, 0x04120008, 0x04020108, 0x04120108, + 0x04021000, 0x04121000, 0x04021100, 0x04121100, + 0x04021008, 0x04121008, 0x04021108, 0x04121108, + }, + { + /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ + 0x00000000, 0x10000000, 0x00010000, 0x10010000, + 0x00000004, 0x10000004, 0x00010004, 0x10010004, + 0x20000000, 0x30000000, 0x20010000, 0x30010000, + 0x20000004, 0x30000004, 0x20010004, 0x30010004, + 0x00100000, 0x10100000, 0x00110000, 0x10110000, + 0x00100004, 0x10100004, 0x00110004, 0x10110004, + 0x20100000, 0x30100000, 0x20110000, 0x30110000, + 0x20100004, 0x30100004, 0x20110004, 0x30110004, + 0x00001000, 0x10001000, 0x00011000, 0x10011000, + 0x00001004, 0x10001004, 0x00011004, 0x10011004, + 0x20001000, 0x30001000, 0x20011000, 0x30011000, + 0x20001004, 0x30001004, 0x20011004, 0x30011004, + 0x00101000, 0x10101000, 0x00111000, 0x10111000, + 0x00101004, 0x10101004, 0x00111004, 0x10111004, + 0x20101000, 0x30101000, 0x20111000, 0x30111000, + 0x20101004, 0x30101004, 0x20111004, 0x30111004, + }, + { + /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ + 0x00000000, 0x08000000, 0x00000008, 0x08000008, + 0x00000400, 0x08000400, 0x00000408, 0x08000408, + 0x00020000, 0x08020000, 0x00020008, 0x08020008, + 0x00020400, 0x08020400, 0x00020408, 0x08020408, + 0x00000001, 0x08000001, 0x00000009, 0x08000009, + 0x00000401, 0x08000401, 0x00000409, 0x08000409, + 0x00020001, 0x08020001, 0x00020009, 0x08020009, + 0x00020401, 0x08020401, 0x00020409, 0x08020409, + 0x02000000, 0x0A000000, 0x02000008, 0x0A000008, + 0x02000400, 0x0A000400, 0x02000408, 0x0A000408, + 0x02020000, 0x0A020000, 0x02020008, 0x0A020008, + 0x02020400, 0x0A020400, 0x02020408, 0x0A020408, + 0x02000001, 0x0A000001, 0x02000009, 0x0A000009, + 0x02000401, 0x0A000401, 0x02000409, 0x0A000409, + 0x02020001, 0x0A020001, 0x02020009, 0x0A020009, + 0x02020401, 0x0A020401, 0x02020409, 0x0A020409, + }, + { + /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ + 0x00000000, 0x00000100, 0x00080000, 0x00080100, + 0x01000000, 0x01000100, 0x01080000, 0x01080100, + 0x00000010, 0x00000110, 0x00080010, 0x00080110, + 0x01000010, 0x01000110, 0x01080010, 0x01080110, + 0x00200000, 0x00200100, 0x00280000, 0x00280100, + 0x01200000, 0x01200100, 0x01280000, 0x01280100, + 0x00200010, 0x00200110, 0x00280010, 0x00280110, + 0x01200010, 0x01200110, 0x01280010, 0x01280110, + 0x00000200, 0x00000300, 0x00080200, 0x00080300, + 0x01000200, 0x01000300, 0x01080200, 0x01080300, + 0x00000210, 0x00000310, 0x00080210, 0x00080310, + 0x01000210, 0x01000310, 0x01080210, 0x01080310, + 0x00200200, 0x00200300, 0x00280200, 0x00280300, + 0x01200200, 0x01200300, 0x01280200, 0x01280300, + 0x00200210, 0x00200310, 0x00280210, 0x00280310, + 0x01200210, 0x01200310, 0x01280210, 0x01280310, + }, + { + /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ + 0x00000000, 0x04000000, 0x00040000, 0x04040000, + 0x00000002, 0x04000002, 0x00040002, 0x04040002, + 0x00002000, 0x04002000, 0x00042000, 0x04042000, + 0x00002002, 0x04002002, 0x00042002, 0x04042002, + 0x00000020, 0x04000020, 0x00040020, 0x04040020, + 0x00000022, 0x04000022, 0x00040022, 0x04040022, + 0x00002020, 0x04002020, 0x00042020, 0x04042020, + 0x00002022, 0x04002022, 0x00042022, 0x04042022, + 0x00000800, 0x04000800, 0x00040800, 0x04040800, + 0x00000802, 0x04000802, 0x00040802, 0x04040802, + 0x00002800, 0x04002800, 0x00042800, 0x04042800, + 0x00002802, 0x04002802, 0x00042802, 0x04042802, + 0x00000820, 0x04000820, 0x00040820, 0x04040820, + 0x00000822, 0x04000822, 0x00040822, 0x04040822, + 0x00002820, 0x04002820, 0x00042820, 0x04042820, + 0x00002822, 0x04002822, 0x00042822, 0x04042822, + } + }; + + private static final int SPtrans[][] = { + { + /* nibble 0 */ + 0x00820200, 0x00020000, 0x80800000, 0x80820200, + 0x00800000, 0x80020200, 0x80020000, 0x80800000, + 0x80020200, 0x00820200, 0x00820000, 0x80000200, + 0x80800200, 0x00800000, 0x00000000, 0x80020000, + 0x00020000, 0x80000000, 0x00800200, 0x00020200, + 0x80820200, 0x00820000, 0x80000200, 0x00800200, + 0x80000000, 0x00000200, 0x00020200, 0x80820000, + 0x00000200, 0x80800200, 0x80820000, 0x00000000, + 0x00000000, 0x80820200, 0x00800200, 0x80020000, + 0x00820200, 0x00020000, 0x80000200, 0x00800200, + 0x80820000, 0x00000200, 0x00020200, 0x80800000, + 0x80020200, 0x80000000, 0x80800000, 0x00820000, + 0x80820200, 0x00020200, 0x00820000, 0x80800200, + 0x00800000, 0x80000200, 0x80020000, 0x00000000, + 0x00020000, 0x00800000, 0x80800200, 0x00820200, + 0x80000000, 0x80820000, 0x00000200, 0x80020200, + }, + { + /* nibble 1 */ + 0x10042004, 0x00000000, 0x00042000, 0x10040000, + 0x10000004, 0x00002004, 0x10002000, 0x00042000, + 0x00002000, 0x10040004, 0x00000004, 0x10002000, + 0x00040004, 0x10042000, 0x10040000, 0x00000004, + 0x00040000, 0x10002004, 0x10040004, 0x00002000, + 0x00042004, 0x10000000, 0x00000000, 0x00040004, + 0x10002004, 0x00042004, 0x10042000, 0x10000004, + 0x10000000, 0x00040000, 0x00002004, 0x10042004, + 0x00040004, 0x10042000, 0x10002000, 0x00042004, + 0x10042004, 0x00040004, 0x10000004, 0x00000000, + 0x10000000, 0x00002004, 0x00040000, 0x10040004, + 0x00002000, 0x10000000, 0x00042004, 0x10002004, + 0x10042000, 0x00002000, 0x00000000, 0x10000004, + 0x00000004, 0x10042004, 0x00042000, 0x10040000, + 0x10040004, 0x00040000, 0x00002004, 0x10002000, + 0x10002004, 0x00000004, 0x10040000, 0x00042000, + }, + { + /* nibble 2 */ + 0x41000000, 0x01010040, 0x00000040, 0x41000040, + 0x40010000, 0x01000000, 0x41000040, 0x00010040, + 0x01000040, 0x00010000, 0x01010000, 0x40000000, + 0x41010040, 0x40000040, 0x40000000, 0x41010000, + 0x00000000, 0x40010000, 0x01010040, 0x00000040, + 0x40000040, 0x41010040, 0x00010000, 0x41000000, + 0x41010000, 0x01000040, 0x40010040, 0x01010000, + 0x00010040, 0x00000000, 0x01000000, 0x40010040, + 0x01010040, 0x00000040, 0x40000000, 0x00010000, + 0x40000040, 0x40010000, 0x01010000, 0x41000040, + 0x00000000, 0x01010040, 0x00010040, 0x41010000, + 0x40010000, 0x01000000, 0x41010040, 0x40000000, + 0x40010040, 0x41000000, 0x01000000, 0x41010040, + 0x00010000, 0x01000040, 0x41000040, 0x00010040, + 0x01000040, 0x00000000, 0x41010000, 0x40000040, + 0x41000000, 0x40010040, 0x00000040, 0x01010000, + }, + { + /* nibble 3 */ + 0x00100402, 0x04000400, 0x00000002, 0x04100402, + 0x00000000, 0x04100000, 0x04000402, 0x00100002, + 0x04100400, 0x04000002, 0x04000000, 0x00000402, + 0x04000002, 0x00100402, 0x00100000, 0x04000000, + 0x04100002, 0x00100400, 0x00000400, 0x00000002, + 0x00100400, 0x04000402, 0x04100000, 0x00000400, + 0x00000402, 0x00000000, 0x00100002, 0x04100400, + 0x04000400, 0x04100002, 0x04100402, 0x00100000, + 0x04100002, 0x00000402, 0x00100000, 0x04000002, + 0x00100400, 0x04000400, 0x00000002, 0x04100000, + 0x04000402, 0x00000000, 0x00000400, 0x00100002, + 0x00000000, 0x04100002, 0x04100400, 0x00000400, + 0x04000000, 0x04100402, 0x00100402, 0x00100000, + 0x04100402, 0x00000002, 0x04000400, 0x00100402, + 0x00100002, 0x00100400, 0x04100000, 0x04000402, + 0x00000402, 0x04000000, 0x04000002, 0x04100400, + }, + { + /* nibble 4 */ + 0x02000000, 0x00004000, 0x00000100, 0x02004108, + 0x02004008, 0x02000100, 0x00004108, 0x02004000, + 0x00004000, 0x00000008, 0x02000008, 0x00004100, + 0x02000108, 0x02004008, 0x02004100, 0x00000000, + 0x00004100, 0x02000000, 0x00004008, 0x00000108, + 0x02000100, 0x00004108, 0x00000000, 0x02000008, + 0x00000008, 0x02000108, 0x02004108, 0x00004008, + 0x02004000, 0x00000100, 0x00000108, 0x02004100, + 0x02004100, 0x02000108, 0x00004008, 0x02004000, + 0x00004000, 0x00000008, 0x02000008, 0x02000100, + 0x02000000, 0x00004100, 0x02004108, 0x00000000, + 0x00004108, 0x02000000, 0x00000100, 0x00004008, + 0x02000108, 0x00000100, 0x00000000, 0x02004108, + 0x02004008, 0x02004100, 0x00000108, 0x00004000, + 0x00004100, 0x02004008, 0x02000100, 0x00000108, + 0x00000008, 0x00004108, 0x02004000, 0x02000008, + }, + { + /* nibble 5 */ + 0x20000010, 0x00080010, 0x00000000, 0x20080800, + 0x00080010, 0x00000800, 0x20000810, 0x00080000, + 0x00000810, 0x20080810, 0x00080800, 0x20000000, + 0x20000800, 0x20000010, 0x20080000, 0x00080810, + 0x00080000, 0x20000810, 0x20080010, 0x00000000, + 0x00000800, 0x00000010, 0x20080800, 0x20080010, + 0x20080810, 0x20080000, 0x20000000, 0x00000810, + 0x00000010, 0x00080800, 0x00080810, 0x20000800, + 0x00000810, 0x20000000, 0x20000800, 0x00080810, + 0x20080800, 0x00080010, 0x00000000, 0x20000800, + 0x20000000, 0x00000800, 0x20080010, 0x00080000, + 0x00080010, 0x20080810, 0x00080800, 0x00000010, + 0x20080810, 0x00080800, 0x00080000, 0x20000810, + 0x20000010, 0x20080000, 0x00080810, 0x00000000, + 0x00000800, 0x20000010, 0x20000810, 0x20080800, + 0x20080000, 0x00000810, 0x00000010, 0x20080010, + }, + { + /* nibble 6 */ + 0x00001000, 0x00000080, 0x00400080, 0x00400001, + 0x00401081, 0x00001001, 0x00001080, 0x00000000, + 0x00400000, 0x00400081, 0x00000081, 0x00401000, + 0x00000001, 0x00401080, 0x00401000, 0x00000081, + 0x00400081, 0x00001000, 0x00001001, 0x00401081, + 0x00000000, 0x00400080, 0x00400001, 0x00001080, + 0x00401001, 0x00001081, 0x00401080, 0x00000001, + 0x00001081, 0x00401001, 0x00000080, 0x00400000, + 0x00001081, 0x00401000, 0x00401001, 0x00000081, + 0x00001000, 0x00000080, 0x00400000, 0x00401001, + 0x00400081, 0x00001081, 0x00001080, 0x00000000, + 0x00000080, 0x00400001, 0x00000001, 0x00400080, + 0x00000000, 0x00400081, 0x00400080, 0x00001080, + 0x00000081, 0x00001000, 0x00401081, 0x00400000, + 0x00401080, 0x00000001, 0x00001001, 0x00401081, + 0x00400001, 0x00401080, 0x00401000, 0x00001001, + }, + { + /* nibble 7 */ + 0x08200020, 0x08208000, 0x00008020, 0x00000000, + 0x08008000, 0x00200020, 0x08200000, 0x08208020, + 0x00000020, 0x08000000, 0x00208000, 0x00008020, + 0x00208020, 0x08008020, 0x08000020, 0x08200000, + 0x00008000, 0x00208020, 0x00200020, 0x08008000, + 0x08208020, 0x08000020, 0x00000000, 0x00208000, + 0x08000000, 0x00200000, 0x08008020, 0x08200020, + 0x00200000, 0x00008000, 0x08208000, 0x00000020, + 0x00200000, 0x00008000, 0x08000020, 0x08208020, + 0x00008020, 0x08000000, 0x00000000, 0x00208000, + 0x08200020, 0x08008020, 0x08008000, 0x00200020, + 0x08208000, 0x00000020, 0x00200020, 0x08008000, + 0x08208020, 0x00200000, 0x08200000, 0x08000020, + 0x00208000, 0x00008020, 0x08008020, 0x08200000, + 0x00000020, 0x08208000, 0x00208020, 0x00000000, + 0x08000000, 0x08200020, 0x00008000, 0x00208020 + } + }; + + private static final int byteToUnsigned(byte b) { + int value = (int) b; + return (value >= 0) ? value : value + 256; + } + + private static int fourBytesToInt(byte b[], int offset) { + return byteToUnsigned(b[offset++]) + | (byteToUnsigned(b[offset++]) << 8) + | (byteToUnsigned(b[offset++]) << 16) + | (byteToUnsigned(b[offset]) << 24); + } + + private static final void intToFourBytes(int iValue, byte b[], int offset) { + b[offset++] = (byte)((iValue) & 0xff); + b[offset++] = (byte)((iValue >>> 8 ) & 0xff); + b[offset++] = (byte)((iValue >>> 16) & 0xff); + b[offset] = (byte)((iValue >>> 24) & 0xff); + } + + private static final void PERM_OP(int a, int b, int n, int m, int results[]) { + int t; + + t = ((a >>> n) ^ b) & m; + a ^= t << n; + b ^= t; + + results[0] = a; + results[1] = b; + } + + private static final int HPERM_OP(int a, int n, int m) { + int t; + + t = ((a << (16 - n)) ^ a) & m; + a = a ^ t ^ (t >>> (16 - n)); + + return a; + } + + private static int [] des_set_key(byte key[]) { + int schedule[] = new int [ITERATIONS * 2]; + + int c = fourBytesToInt(key, 0); + int d = fourBytesToInt(key, 4); + + int results[] = new int[2]; + + PERM_OP(d, c, 4, 0x0f0f0f0f, results); + d = results[0]; c = results[1]; + + c = HPERM_OP(c, -2, 0xcccc0000); + d = HPERM_OP(d, -2, 0xcccc0000); + + PERM_OP(d, c, 1, 0x55555555, results); + d = results[0]; c = results[1]; + + PERM_OP(c, d, 8, 0x00ff00ff, results); + c = results[0]; d = results[1]; + + PERM_OP(d, c, 1, 0x55555555, results); + d = results[0]; c = results[1]; + + d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) | + ((d & 0x00ff0000) >>> 16) | ((c & 0xf0000000) >>> 4)); + c &= 0x0fffffff; + + int s, t; + int j = 0; + + for(int i = 0; i < ITERATIONS; i ++) { + if(shifts2[i]) { + c = (c >>> 2) | (c << 26); + d = (d >>> 2) | (d << 26); + } else { + c = (c >>> 1) | (c << 27); + d = (d >>> 1) | (d << 27); + } + + c &= 0x0fffffff; + d &= 0x0fffffff; + + s = skb[0][ (c ) & 0x3f ]| + skb[1][((c >>> 6) & 0x03) | ((c >>> 7) & 0x3c)]| + skb[2][((c >>> 13) & 0x0f) | ((c >>> 14) & 0x30)]| + skb[3][((c >>> 20) & 0x01) | ((c >>> 21) & 0x06) | + ((c >>> 22) & 0x38)]; + + t = skb[4][ (d ) & 0x3f ]| + skb[5][((d >>> 7) & 0x03) | ((d >>> 8) & 0x3c)]| + skb[6][ (d >>>15) & 0x3f ]| + skb[7][((d >>>21) & 0x0f) | ((d >>> 22) & 0x30)]; + + schedule[j++] = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff; + s = ((s >>> 16) | (t & 0xffff0000)); + + s = (s << 4) | (s >>> 28); + schedule[j++] = s & 0xffffffff; + } + return schedule; + } + + private static final int D_ENCRYPT(int L, int R, int S, int E0, int E1, int s[]) { + int t, u, v; + + v = R ^ (R >>> 16); + u = v & E0; + v = v & E1; + u = (u ^ (u << 16)) ^ R ^ s[S]; + t = (v ^ (v << 16)) ^ R ^ s[S + 1]; + t = (t >>> 4) | (t << 28); + + L ^= SPtrans[1][(t ) & 0x3f] | + SPtrans[3][(t >>> 8) & 0x3f] | + SPtrans[5][(t >>> 16) & 0x3f] | + SPtrans[7][(t >>> 24) & 0x3f] | + SPtrans[0][(u ) & 0x3f] | + SPtrans[2][(u >>> 8) & 0x3f] | + SPtrans[4][(u >>> 16) & 0x3f] | + SPtrans[6][(u >>> 24) & 0x3f]; + + return L; + } + + private static final int [] body(int schedule[], int Eswap0, int Eswap1) { + int left = 0; + int right = 0; + int t = 0; + + for (int j = 0; j < 25; j ++) { + for (int i = 0; i < ITERATIONS * 2; i += 4) { + left = D_ENCRYPT(left, right, i, Eswap0, Eswap1, schedule); + right = D_ENCRYPT(right, left, i + 2, Eswap0, Eswap1, schedule); + } + t = left; + left = right; + right = t; + } + + t = right; + + right = (left >>> 1) | (left << 31); + left = (t >>> 1) | (t << 31); + + left &= 0xffffffff; + right &= 0xffffffff; + + int results[] = new int[2]; + + PERM_OP(right, left, 1, 0x55555555, results); + right = results[0]; left = results[1]; + + PERM_OP(left, right, 8, 0x00ff00ff, results); + left = results[0]; right = results[1]; + + PERM_OP(right, left, 2, 0x33333333, results); + right = results[0]; left = results[1]; + + PERM_OP(left, right, 16, 0x0000ffff, results); + left = results[0]; right = results[1]; + + PERM_OP(right, left, 4, 0x0f0f0f0f, results); + right = results[0]; left = results[1]; + + int out[] = new int[2]; + + out[0] = left; + out[1] = right; + + return out; + } + + public static final String alphabet = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + public static final String crypt(String salt, String original) { + // wwb -- Should do some sanity checks: salt needs to be 2 chars, in alpha. + while(salt.length() < 2) + salt += "A"; + + char[] buffer = new char [13]; + + char charZero = salt.charAt(0); + char charOne = salt.charAt(1); + + buffer[0] = charZero; + buffer[1] = charOne; + + int Eswap0 = alphabet.indexOf(charZero); + int Eswap1 = alphabet.indexOf(charOne) << 4; + byte key[] = new byte[8]; + + for(int i = 0; i < key.length; i ++) + key[i] = (byte)0; + + for(int i = 0; i < key.length && i < original.length(); i ++) + key[i] = (byte) (((int) original.charAt(i)) << 1); + + int schedule[] = des_set_key(key); + int out[] = body(schedule, Eswap0, Eswap1); + + byte b[] = new byte[9]; + + intToFourBytes(out[0], b, 0); + intToFourBytes(out[1], b, 4); + b[8] = 0; + + for(int i = 2, y = 0, u = 0x80; i < 13; i ++) { + for(int j = 0, c = 0; j < 6; j ++) { + c <<= 1; + + if(((int)b[y] & u) != 0) + c |= 1; + + u >>>= 1; + + if (u == 0) { + y++; + u = 0x80; + } + buffer[i] = alphabet.charAt(c); + } + } + return new String(buffer); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/DateUtil.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/DateUtil.java new file mode 100644 index 00000000..5d0e240d --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/DateUtil.java @@ -0,0 +1,17 @@ +package com.gluster.storage.management.core.utils; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateUtil { + public static final String formatDate(Date inputDate) { + DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); + return formatter.format(inputDate); + } + + public static final String formatTime(Date inputDate) { + DateFormat formatter = new SimpleDateFormat("HH:mm:ss z"); + return formatter.format(inputDate); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java new file mode 100644 index 00000000..f3296c5c --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/FileUtil.java @@ -0,0 +1,22 @@ +package com.gluster.storage.management.core.utils; + +import java.io.File; +import java.io.FileInputStream; + +import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; + +public class FileUtil { + public String readFileAsString(File file) { + try { + FileInputStream fileInputStream = new FileInputStream(file); + byte[] data = new byte[fileInputStream.available()]; + fileInputStream.read(data); + fileInputStream.close(); + + return new String(data); + } catch (Exception e) { + e.printStackTrace(); + throw new GlusterRuntimeException("Could not read file [" + file + "]", e); + } + } +}
\ No newline at end of file diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/MD5.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/MD5.java new file mode 100644 index 00000000..c4d6abb8 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/MD5.java @@ -0,0 +1,505 @@ +package com.gluster.storage.management.core.utils; + +/* + * MD5 in Java JDK Beta-2 + * written Santeri Paavolainen, Helsinki Finland 1996 + * (c) Santeri Paavolainen, Helsinki Finland 1996 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * See http://www.cs.hut.fi/~santtu/java/ for more information on this + * class. + * + * This is rather straight re-implementation of the reference implementation + * given in RFC1321 by RSA. + * + * Passes MD5 test suite as defined in RFC1321. + * + * + * This Java class has been derivedfrom the RSA Data Security, Inc. MD5 + * Message-Digest Algorithm and its reference implementation. + * + * + * $Log: MD5.java,v $ + * Revision 1.1 2004/07/12 13:35:20 aubryp + * initial commit + * + * Revision 1.3 2002/03/16 01:46:39 broccol + * Moved the MD5 classes into the md5 package to make 1.4 javac happy + * + * Revision 1.2 1999/11/04 21:38:00 broccol + * Got MD5Crypt calculating the same hash as the OpenBSD md5crypt.c routine. + * + * Revision 1.1 1999/08/05 22:07:03 broccol + * Added support for the MD5 classes. + * + * Revision 1.3 1996/04/15 07:28:09 santtu + * Added GPL statements, and RSA derivate statements. + * + * Revision 1.2 1996/03/04 08:05:48 santtu + * Added offsets to Update method + * + * Revision 1.1 1996/01/07 20:51:59 santtu + * Initial revision + * + */ + +/** + * Contains internal state of the MD5 class + * + * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi> + */ + +class MD5State { + /** + * 128-byte state + */ + int state[]; + + /** + * 64-bit character count (could be true Java long?) + */ + int count[]; + + /** + * 64-byte buffer (512 bits) for storing to-be-hashed characters + */ + byte buffer[]; + + public MD5State() { + buffer = new byte[64]; + count = new int[2]; + state = new int[4]; + + state[0] = 0x67452301; + state[1] = 0xefcdab89; + state[2] = 0x98badcfe; + state[3] = 0x10325476; + + count[0] = count[1] = 0; + } + + /** Create this State as a copy of another state */ + public MD5State(MD5State from) { + this (); + + int i; + + for (i = 0; i < buffer.length; i++) + this .buffer[i] = from.buffer[i]; + + for (i = 0; i < state.length; i++) + this .state[i] = from.state[i]; + + for (i = 0; i < count.length; i++) + this .count[i] = from.count[i]; + } +}; + +/** + * Implementation of RSA's MD5 hash generator + * + * @version $Revision: 1.1 $ + * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi> + */ + +public class MD5 { + /** + * MD5 state + */ + MD5State state; + + /** + * If Final() has been called, finals is set to the current finals + * state. Any Update() causes this to be set to null. + */ + MD5State finals; + + /** + * Padding for Final() + */ + static byte padding[] = { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + /** + * Initialize MD5 internal state (object can be reused just by + * calling Init() after every Final() + */ + public synchronized void Init() { + state = new MD5State(); + finals = null; + } + + /** + * Class constructor + */ + public MD5() { + this .Init(); + } + + /** + * Initialize class, and update hash with ob.toString() + * + * @param ob Object, ob.toString() is used to update hash + * after initialization + */ + public MD5(Object ob) { + this (); + Update(ob.toString()); + } + + public String debugDump() { + return asHex(); + } + + private int rotate_left(int x, int n) { + return (x << n) | (x >>> (32 - n)); + } + + /* I wonder how many loops and hoops you'll have to go through to + get unsigned add for longs in java */ + + private int uadd(int a, int b) { + long aa, bb; + aa = ((long) a) & 0xffffffffL; + bb = ((long) b) & 0xffffffffL; + + aa += bb; + + return (int) (aa & 0xffffffffL); + } + + private int uadd(int a, int b, int c) { + return uadd(uadd(a, b), c); + } + + private int uadd(int a, int b, int c, int d) { + return uadd(uadd(a, b, c), d); + } + + private int FF(int a, int b, int c, int d, int x, int s, int ac) { + a = uadd(a, ((b & c) | (~b & d)), x, ac); + return uadd(rotate_left(a, s), b); + } + + private int GG(int a, int b, int c, int d, int x, int s, int ac) { + a = uadd(a, ((b & d) | (c & ~d)), x, ac); + return uadd(rotate_left(a, s), b); + } + + private int HH(int a, int b, int c, int d, int x, int s, int ac) { + a = uadd(a, (b ^ c ^ d), x, ac); + return uadd(rotate_left(a, s), b); + } + + private int II(int a, int b, int c, int d, int x, int s, int ac) { + a = uadd(a, (c ^ (b | ~d)), x, ac); + return uadd(rotate_left(a, s), b); + } + + private int[] Decode(byte buffer[], int len, int shift) { + int out[]; + int i, j; + + out = new int[16]; + + for (i = j = 0; j < len; i++, j += 4) { + out[i] = ((int) (buffer[j + shift] & 0xff)) + | (((int) (buffer[j + 1 + shift] & 0xff)) << 8) + | (((int) (buffer[j + 2 + shift] & 0xff)) << 16) + | (((int) (buffer[j + 3 + shift] & 0xff)) << 24); + } + + return out; + } + + private void Transform(MD5State state, byte buffer[], int shift) { + int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3], x[]; + + x = Decode(buffer, 64, shift); + + /* Round 1 */ + a = FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */ + d = FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */ + c = FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */ + b = FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */ + a = FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */ + d = FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */ + c = FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */ + b = FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */ + a = FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */ + d = FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */ + c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */ + b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */ + a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */ + d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */ + c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */ + b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */ + + /* Round 2 */ + a = GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */ + d = GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */ + c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */ + b = GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */ + a = GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */ + d = GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */ + c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */ + b = GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */ + a = GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */ + d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */ + c = GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */ + b = GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */ + a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */ + d = GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */ + c = GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */ + b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + a = HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */ + d = HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */ + c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */ + b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */ + a = HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */ + d = HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */ + c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */ + b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */ + a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */ + d = HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */ + c = HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */ + b = HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */ + a = HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */ + d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */ + c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */ + b = HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + a = II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */ + d = II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */ + c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */ + b = II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */ + a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */ + d = II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */ + c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */ + b = II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */ + a = II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */ + d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */ + c = II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */ + b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */ + a = II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */ + d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */ + c = II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */ + b = II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */ + + state.state[0] += a; + state.state[1] += b; + state.state[2] += c; + state.state[3] += d; + } + + /** + * Updates hash with the bytebuffer given (using at maximum length bytes from + * that buffer) + * + * @param stat Which state is updated + * @param buffer Array of bytes to be hashed + * @param offset Offset to buffer array + * @param length Use at maximum `length' bytes (absolute + * maximum is buffer.length) + */ + public void Update(MD5State stat, byte buffer[], int offset, + int length) { + int index, partlen, i, start; + + finals = null; + + /* Length can be told to be shorter, but not inter */ + if ((length - offset) > buffer.length) + length = buffer.length - offset; + + /* compute number of bytes mod 64 */ + index = (int) (stat.count[0] >>> 3) & 0x3f; + + if ((stat.count[0] += (length << 3)) < (length << 3)) + stat.count[1]++; + + stat.count[1] += length >>> 29; + + partlen = 64 - index; + + if (length >= partlen) { + for (i = 0; i < partlen; i++) + stat.buffer[i + index] = buffer[i + offset]; + + Transform(stat, stat.buffer, 0); + + for (i = partlen; (i + 63) < length; i += 64) + Transform(stat, buffer, i); + + index = 0; + } else + i = 0; + + /* buffer remaining input */ + if (i < length) { + start = i; + for (; i < length; i++) + stat.buffer[index + i - start] = buffer[i + offset]; + } + } + + /* + * Update()s for other datatypes than byte[] also. Update(byte[], int) + * is only the main driver. + */ + + /** + * Plain update, updates this object + */ + + public void Update(byte buffer[], int offset, int length) { + Update(this .state, buffer, offset, length); + } + + public void Update(byte buffer[], int length) { + Update(this .state, buffer, 0, length); + } + + /** + * Updates hash with given array of bytes + * + * @param buffer Array of bytes to use for updating the hash + */ + public void Update(byte buffer[]) { + Update(buffer, 0, buffer.length); + } + + /** + * Updates hash with a single byte + * + * @param b Single byte to update the hash + */ + public void Update(byte b) { + byte buffer[] = new byte[1]; + buffer[0] = b; + + Update(buffer, 1); + } + + /** + * Update buffer with given string. + * + * @param s String to be update to hash (is used as + * s.getBytes()) + */ + public void Update(String s) { + byte chars[]; + + chars = s.getBytes(); + + Update(chars, chars.length); + } + + private byte[] Encode(int input[], int len) { + int i, j; + byte out[]; + + out = new byte[len]; + + for (i = j = 0; j < len; i++, j += 4) { + out[j] = (byte) (input[i] & 0xff); + out[j + 1] = (byte) ((input[i] >>> 8) & 0xff); + out[j + 2] = (byte) ((input[i] >>> 16) & 0xff); + out[j + 3] = (byte) ((input[i] >>> 24) & 0xff); + } + + return out; + } + + /** + * Returns array of bytes (16 bytes) representing hash as of the + * current state of this object. Note: getting a hash does not + * invalidate the hash object, it only creates a copy of the real + * state which is finalized. + * + * @return Array of 16 bytes, the hash of all updated bytes + */ + public synchronized byte[] Final() { + byte bits[]; + int index, padlen; + MD5State fin; + + if (finals == null) { + fin = new MD5State(state); + + bits = Encode(fin.count, 8); + + index = (int) ((fin.count[0] >>> 3) & 0x3f); + padlen = (index < 56) ? (56 - index) : (120 - index); + + Update(fin, padding, 0, padlen); + /**/ + Update(fin, bits, 0, 8); + + /* Update() sets finalds to null */ + finals = fin; + } + + return Encode(finals.state, 16); + } + + /** + * Turns array of bytes into string representing each byte as + * unsigned hex number. + * + * @param hash Array of bytes to convert to hex-string + * @return Generated hex string + */ + public static String asHex(byte hash[]) { + StringBuffer buf = new StringBuffer(hash.length * 2); + int i; + + for (i = 0; i < hash.length; i++) { + if (((int) hash[i] & 0xff) < 0x10) + buf.append("0"); + + buf.append(Long.toString((int) hash[i] & 0xff, 16)); + } + + return buf.toString(); + } + + /** + * Returns 32-character hex representation of this objects hash + * + * @return String of this object's hash + */ + public String asHex() { + return asHex(this .Final()); + } + + /** + * One-stop md5 string encrypting. + */ + + public static String md5crypt(String input) { + MD5 md5 = new MD5(); + md5.Init(); + md5.Update(input); + return md5.asHex(); + } +}
\ No newline at end of file diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/MD5Crypt.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/MD5Crypt.java new file mode 100644 index 00000000..443053b2 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/MD5Crypt.java @@ -0,0 +1,341 @@ +package com.gluster.storage.management.core.utils; + + +/*------------------------------------------------------------------------------ + class + MD5Crypt + + ------------------------------------------------------------------------------*/ + +/** + * <p>This class defines a method, + * {@link MD5Crypt#crypt(java.lang.String, java.lang.String) crypt()}, which + * takes a password and a salt string and generates an OpenBSD/FreeBSD/Linux-compatible + * md5-encoded password entry.</p> + * + * <p>Created: 3 November 1999</p> + * <p>Release: $Name: $</p> + * <p>Version: $Revision: 1.1 $</p> + * <p>Last Mod Date: $Date: 2004/07/12 13:35:20 $</p> + * <p>Java Code By: Jonathan Abbey, jonabbey@arlut.utexas.edu</p> + * <p>Original C Version:<pre> + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + * </pre></p> + * + * @author Jonathan Abbey <jonabbey at arlut.utexas.edu> + */ + +public final class MD5Crypt { + + /** + * + * Command line test rig. + * + */ + + static public void main(String argv[]) { + if ((argv.length < 1) || (argv.length > 3)) { + System.err + .println("Usage: MD5Crypt [-apache] password salt"); + System.exit(1); + } + + if (argv.length == 3) { + System.err.println(MD5Crypt.apacheCrypt(argv[1], argv[2])); + } else if (argv.length == 2) { + System.err.println(MD5Crypt.crypt(argv[0], argv[1])); + } else { + System.err.println(MD5Crypt.crypt(argv[0])); + } + + System.exit(0); + } + + static private final String SALTCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + + static private final String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + static private final String to64(long v, int size) { + StringBuffer result = new StringBuffer(); + + while (--size >= 0) { + result.append(itoa64.charAt((int) (v & 0x3f))); + v >>>= 6; + } + + return result.toString(); + } + + static private final void clearbits(byte bits[]) { + for (int i = 0; i < bits.length; i++) { + bits[i] = 0; + } + } + + /** + * convert an encoded unsigned byte value into a int + * with the unsigned value. + */ + + static private final int bytes2u(byte inp) { + return (int) inp & 0xff; + } + + /** + * <p>This method actually generates a OpenBSD/FreeBSD/Linux PAM compatible + * md5-encoded password hash from a plaintext password and a + * salt.</p> + * + * <p>The resulting string will be in the form '$1$<salt>$<hashed mess></p> + * + * @param password Plaintext password + * + * @return An OpenBSD/FreeBSD/Linux-compatible md5-hashed password field. + */ + + static public final String crypt(String password) { + StringBuffer salt = new StringBuffer(); + java.util.Random randgen = new java.util.Random(); + + /* -- */ + + while (salt.length() < 8) { + int index = (int) (randgen.nextFloat() * SALTCHARS.length()); + salt.append(SALTCHARS.substring(index, index + 1)); + } + + return MD5Crypt.crypt(password, salt.toString()); + } + + /** + * <p>This method actually generates a OpenBSD/FreeBSD/Linux PAM compatible + * md5-encoded password hash from a plaintext password and a + * salt.</p> + * + * <p>The resulting string will be in the form '$1$<salt>$<hashed mess></p> + * + * @param password Plaintext password + * @param salt A short string to use to randomize md5. May start with $1$, which + * will be ignored. It is explicitly permitted to pass a pre-existing + * MD5Crypt'ed password entry as the salt. crypt() will strip the salt + * chars out properly. + * + * @return An OpenBSD/FreeBSD/Linux-compatible md5-hashed password field. + */ + + static public final String crypt(String password, String salt) { + return MD5Crypt.crypt(password, salt, "$1$"); + } + + /** + * <p>This method generates an Apache MD5 compatible + * md5-encoded password hash from a plaintext password and a + * salt.</p> + * + * <p>The resulting string will be in the form '$apr1$<salt>$<hashed mess></p> + * + * @param password Plaintext password + * + * @return An Apache-compatible md5-hashed password string. + */ + + static public final String apacheCrypt(String password) { + StringBuffer salt = new StringBuffer(); + java.util.Random randgen = new java.util.Random(); + + /* -- */ + + while (salt.length() < 8) { + int index = (int) (randgen.nextFloat() * SALTCHARS.length()); + salt.append(SALTCHARS.substring(index, index + 1)); + } + + return MD5Crypt.apacheCrypt(password, salt.toString()); + } + + /** + * <p>This method actually generates an Apache MD5 compatible + * md5-encoded password hash from a plaintext password and a + * salt.</p> + * + * <p>The resulting string will be in the form '$apr1$<salt>$<hashed mess></p> + * + * @param password Plaintext password + * @param salt A short string to use to randomize md5. May start with $apr1$, which + * will be ignored. It is explicitly permitted to pass a pre-existing + * MD5Crypt'ed password entry as the salt. crypt() will strip the salt + * chars out properly. + * + * @return An Apache-compatible md5-hashed password string. + */ + + static public final String apacheCrypt(String password, String salt) { + return MD5Crypt.crypt(password, salt, "$apr1$"); + } + + /** + * <p>This method actually generates md5-encoded password hash from + * a plaintext password, a salt, and a magic string.</p> + * + * <p>There are two magic strings that make sense to use here.. '$1$' is the + * magic string used by the FreeBSD/Linux/OpenBSD MD5Crypt algorithm, and + * '$apr1$' is the magic string used by the Apache MD5Crypt algorithm.</p> + * + * <p>The resulting string will be in the form '<magic><salt>$<hashed mess></p> + * + * @param password Plaintext password @param salt A short string to + * use to randomize md5. May start with the magic string, which + * will be ignored. It is explicitly permitted to pass a + * pre-existing MD5Crypt'ed password entry as the salt. crypt() + * will strip the salt chars out properly. + * + * @return An md5-hashed password string. + */ + + static public final String crypt(String password, String salt, + String magic) { + /* This string is magic for this algorithm. Having it this way, + * we can get get better later on */ + + byte finalState[]; + MD5 ctx, ctx1; + long l; + + /* -- */ + + /* Refine the Salt first */ + + /* If it starts with the magic string, then skip that */ + + if (salt.startsWith(magic)) { + salt = salt.substring(magic.length()); + } + + /* It stops at the first '$', max 8 chars */ + + if (salt.indexOf('$') != -1) { + salt = salt.substring(0, salt.indexOf('$')); + } + + if (salt.length() > 8) { + salt = salt.substring(0, 8); + } + + ctx = new MD5(); + + ctx.Update(password); // The password first, since that is what is most unknown + ctx.Update(magic); // Then our magic string + ctx.Update(salt); // Then the raw salt + + /* Then just as many characters of the MD5(pw,salt,pw) */ + + ctx1 = new MD5(); + ctx1.Update(password); + ctx1.Update(salt); + ctx1.Update(password); + finalState = ctx1.Final(); + + for (int pl = password.length(); pl > 0; pl -= 16) { + ctx.Update(finalState, pl > 16 ? 16 : pl); + } + + /* the original code claimed that finalState was being cleared + to keep dangerous bits out of memory, but doing this is also + required in order to get the right output. */ + + clearbits(finalState); + + /* Then something really weird... */ + + for (int i = password.length(); i != 0; i >>>= 1) { + if ((i & 1) != 0) { + ctx.Update(finalState, 1); + } else { + ctx.Update(password.getBytes(), 1); + } + } + + finalState = ctx.Final(); + + /* + * and now, just to make sure things don't run too fast + * On a 60 Mhz Pentium this takes 34 msec, so you would + * need 30 seconds to build a 1000 entry dictionary... + * + * (The above timings from the C version) + */ + + for (int i = 0; i < 1000; i++) { + ctx1 = new MD5(); + + if ((i & 1) != 0) { + ctx1.Update(password); + } else { + ctx1.Update(finalState, 16); + } + + if ((i % 3) != 0) { + ctx1.Update(salt); + } + + if ((i % 7) != 0) { + ctx1.Update(password); + } + + if ((i & 1) != 0) { + ctx1.Update(finalState, 16); + } else { + ctx1.Update(password); + } + + finalState = ctx1.Final(); + } + + /* Now make the output string */ + + StringBuffer result = new StringBuffer(); + + result.append(magic); + result.append(salt); + result.append("$"); + + l = (bytes2u(finalState[0]) << 16) + | (bytes2u(finalState[6]) << 8) + | bytes2u(finalState[12]); + result.append(to64(l, 4)); + + l = (bytes2u(finalState[1]) << 16) + | (bytes2u(finalState[7]) << 8) + | bytes2u(finalState[13]); + result.append(to64(l, 4)); + + l = (bytes2u(finalState[2]) << 16) + | (bytes2u(finalState[8]) << 8) + | bytes2u(finalState[14]); + result.append(to64(l, 4)); + + l = (bytes2u(finalState[3]) << 16) + | (bytes2u(finalState[9]) << 8) + | bytes2u(finalState[15]); + result.append(to64(l, 4)); + + l = (bytes2u(finalState[4]) << 16) + | (bytes2u(finalState[10]) << 8) + | bytes2u(finalState[5]); + result.append(to64(l, 4)); + + l = bytes2u(finalState[11]); + result.append(to64(l, 2)); + + /* Don't leave anything around in vm they could use. */ + clearbits(finalState); + + return result.toString(); + } +} + diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/NumberUtil.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/NumberUtil.java new file mode 100644 index 00000000..fb32121e --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/NumberUtil.java @@ -0,0 +1,12 @@ +package com.gluster.storage.management.core.utils; + +import java.text.NumberFormat; + +public class NumberUtil { + public static final String formatNumber(double num) { + NumberFormat formatter = NumberFormat.getNumberInstance(); + formatter.setMinimumFractionDigits(2); + formatter.setMaximumFractionDigits(2); + return formatter.format(num); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessResult.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessResult.java new file mode 100644 index 00000000..32cd53ba --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessResult.java @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + * This file is part of GlusterFS. + * + * Gluster Storage Platform is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, + * or (at your option) any later version. + * + * GlusterFS is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ +package com.gluster.storage.management.core.utils; + +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Represents the result of a command execution in a separate process. + * Consists of the "exit status" of the process and output from the process. + * The output includes stdout as well as stderr streams + */ +@XmlRootElement +public class ProcessResult { + + public static final int SUCCESS = 0; + private int exitValue; + private String output; + + // Required for JAXB de-serialization + public ProcessResult() { + + } + + public ProcessResult(int exitValue, String output) { + this.exitValue = exitValue; + this.output = output; + } + + public int getExitValue() { + return exitValue; + } + + public void setExitValue(int exitValue) { + this.exitValue = exitValue; + } + + public String getOutput() { + return output; + } + + public void setOutput(String output) { + this.output = output; + } + + public boolean isSuccess() { + return exitValue == SUCCESS; + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java new file mode 100644 index 00000000..2ec8e8a4 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java @@ -0,0 +1,97 @@ +/** + * Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com> + * This file is part of GlusterFS. + * + * GlusterFS is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, + * or (at your option) any later version. + * + * GlusterFS is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ +package com.gluster.storage.management.core.utils; + +import static com.gluster.storage.management.core.constants.CoreConstants.NEWLINE; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; + +/** + * Utility class for creating processes (foreground/background) with given + * command and checking the output/exit status + */ +public class ProcessUtil { + + private static final ProcessUtil instance = new ProcessUtil(); + + public ProcessResult executeCommand(List<String> command) { + return executeCommand(true, command); + } + + /** + * Executes given command in a separate process in FOREGROUND + * @param command + * @return {@link ProcessResult} object + */ + public ProcessResult executeCommand(String... command) { + ArrayList<String> commandList = new ArrayList<String>(); + for (String part : command) { + commandList.add(part); + } + return executeCommand(commandList); + } + + /** + * Executes given command in foreground/background + * @param command + * @param runInForeground Boolean flag indicating whether the command should + * be executed in foreground + * @return {@link ProcessResult} object + */ + public ProcessResult executeCommand(boolean runInForeground, List<String> command) { + StringBuilder output = new StringBuilder(); + try { + Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); + + if (runInForeground) { + process.waitFor(); // Wait for process to finish + + InputStream is = process.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String line; + + while ((line = br.readLine()) != null) { + output.append(line); + output.append(NEWLINE); + } + } else { + output.append("Command ["); + output.append(command); + output.append("] triggerred in background."); + } + + return new ProcessResult(process.exitValue(), output.toString()); + } catch (Throwable e) { + throw new GlusterRuntimeException("Exception while executing command [" + command + "]", e); + } + } + + public static void main(String args[]) { + ProcessResult result = new ProcessUtil().executeCommand("ls", "-lrt", "/"); + System.out.println(result.getExitValue()); + System.out.println(result.getOutput()); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtils.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtils.java new file mode 100644 index 00000000..356fe007 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/StringUtils.java @@ -0,0 +1,10 @@ +package com.gluster.storage.management.core.utils; + +public class StringUtils { + public static boolean filterString(String sourceString, + String filterString, boolean caseSensitive) { + return caseSensitive ? sourceString.contains(filterString) + : sourceString.toLowerCase().contains( + filterString.toLowerCase()); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/UnixCrypt.java b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/UnixCrypt.java new file mode 100644 index 00000000..693ce621 --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/UnixCrypt.java @@ -0,0 +1,684 @@ +/** + * This class provides us with the ability to encrypt passwords when sent + * over the network stream + * + * <P>Contains static methods to encrypt and compare + * passwords with Unix encrypted passwords.</P> + * + * <P>See <A HREF="http://www.zeh.com/local/jfd/crypt.html"> + * John Dumas's Java Crypt page</A> for the original source.</P> + * + * @author jdumas@zgs.com (John Dumas) + */ +package com.gluster.storage.management.core.utils; + +public class UnixCrypt extends Object +{ + // + // Null constructor - can't instantiate class + private UnixCrypt() + { + } + + private static final char[] saltChars = + ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./".toCharArray()); + + private static final int ITERATIONS = 16; + + private static final int con_salt[] = + { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x0A, 0x0B, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, + 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, + 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, + 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, + 0x23, 0x24, 0x25, 0x20, 0x21, 0x22, 0x23, 0x24, + 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, + 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, + 0x3D, 0x3E, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + + private static final boolean shifts2[] = + { + false, false, true, true, true, true, true, true, + false, true, true, true, true, true, true, false + }; + + private static final int skb[][] = + { + { + /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ + 0x00000000, 0x00000010, 0x20000000, 0x20000010, + 0x00010000, 0x00010010, 0x20010000, 0x20010010, + 0x00000800, 0x00000810, 0x20000800, 0x20000810, + 0x00010800, 0x00010810, 0x20010800, 0x20010810, + 0x00000020, 0x00000030, 0x20000020, 0x20000030, + 0x00010020, 0x00010030, 0x20010020, 0x20010030, + 0x00000820, 0x00000830, 0x20000820, 0x20000830, + 0x00010820, 0x00010830, 0x20010820, 0x20010830, + 0x00080000, 0x00080010, 0x20080000, 0x20080010, + 0x00090000, 0x00090010, 0x20090000, 0x20090010, + 0x00080800, 0x00080810, 0x20080800, 0x20080810, + 0x00090800, 0x00090810, 0x20090800, 0x20090810, + 0x00080020, 0x00080030, 0x20080020, 0x20080030, + 0x00090020, 0x00090030, 0x20090020, 0x20090030, + 0x00080820, 0x00080830, 0x20080820, 0x20080830, + 0x00090820, 0x00090830, 0x20090820, 0x20090830, + }, + { + /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ + 0x00000000, 0x02000000, 0x00002000, 0x02002000, + 0x00200000, 0x02200000, 0x00202000, 0x02202000, + 0x00000004, 0x02000004, 0x00002004, 0x02002004, + 0x00200004, 0x02200004, 0x00202004, 0x02202004, + 0x00000400, 0x02000400, 0x00002400, 0x02002400, + 0x00200400, 0x02200400, 0x00202400, 0x02202400, + 0x00000404, 0x02000404, 0x00002404, 0x02002404, + 0x00200404, 0x02200404, 0x00202404, 0x02202404, + 0x10000000, 0x12000000, 0x10002000, 0x12002000, + 0x10200000, 0x12200000, 0x10202000, 0x12202000, + 0x10000004, 0x12000004, 0x10002004, 0x12002004, + 0x10200004, 0x12200004, 0x10202004, 0x12202004, + 0x10000400, 0x12000400, 0x10002400, 0x12002400, + 0x10200400, 0x12200400, 0x10202400, 0x12202400, + 0x10000404, 0x12000404, 0x10002404, 0x12002404, + 0x10200404, 0x12200404, 0x10202404, 0x12202404, + }, + { + /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ + 0x00000000, 0x00000001, 0x00040000, 0x00040001, + 0x01000000, 0x01000001, 0x01040000, 0x01040001, + 0x00000002, 0x00000003, 0x00040002, 0x00040003, + 0x01000002, 0x01000003, 0x01040002, 0x01040003, + 0x00000200, 0x00000201, 0x00040200, 0x00040201, + 0x01000200, 0x01000201, 0x01040200, 0x01040201, + 0x00000202, 0x00000203, 0x00040202, 0x00040203, + 0x01000202, 0x01000203, 0x01040202, 0x01040203, + 0x08000000, 0x08000001, 0x08040000, 0x08040001, + 0x09000000, 0x09000001, 0x09040000, 0x09040001, + 0x08000002, 0x08000003, 0x08040002, 0x08040003, + 0x09000002, 0x09000003, 0x09040002, 0x09040003, + 0x08000200, 0x08000201, 0x08040200, 0x08040201, + 0x09000200, 0x09000201, 0x09040200, 0x09040201, + 0x08000202, 0x08000203, 0x08040202, 0x08040203, + 0x09000202, 0x09000203, 0x09040202, 0x09040203, + }, + { + /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ + 0x00000000, 0x00100000, 0x00000100, 0x00100100, + 0x00000008, 0x00100008, 0x00000108, 0x00100108, + 0x00001000, 0x00101000, 0x00001100, 0x00101100, + 0x00001008, 0x00101008, 0x00001108, 0x00101108, + 0x04000000, 0x04100000, 0x04000100, 0x04100100, + 0x04000008, 0x04100008, 0x04000108, 0x04100108, + 0x04001000, 0x04101000, 0x04001100, 0x04101100, + 0x04001008, 0x04101008, 0x04001108, 0x04101108, + 0x00020000, 0x00120000, 0x00020100, 0x00120100, + 0x00020008, 0x00120008, 0x00020108, 0x00120108, + 0x00021000, 0x00121000, 0x00021100, 0x00121100, + 0x00021008, 0x00121008, 0x00021108, 0x00121108, + 0x04020000, 0x04120000, 0x04020100, 0x04120100, + 0x04020008, 0x04120008, 0x04020108, 0x04120108, + 0x04021000, 0x04121000, 0x04021100, 0x04121100, + 0x04021008, 0x04121008, 0x04021108, 0x04121108, + }, + { + /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ + 0x00000000, 0x10000000, 0x00010000, 0x10010000, + 0x00000004, 0x10000004, 0x00010004, 0x10010004, + 0x20000000, 0x30000000, 0x20010000, 0x30010000, + 0x20000004, 0x30000004, 0x20010004, 0x30010004, + 0x00100000, 0x10100000, 0x00110000, 0x10110000, + 0x00100004, 0x10100004, 0x00110004, 0x10110004, + 0x20100000, 0x30100000, 0x20110000, 0x30110000, + 0x20100004, 0x30100004, 0x20110004, 0x30110004, + 0x00001000, 0x10001000, 0x00011000, 0x10011000, + 0x00001004, 0x10001004, 0x00011004, 0x10011004, + 0x20001000, 0x30001000, 0x20011000, 0x30011000, + 0x20001004, 0x30001004, 0x20011004, 0x30011004, + 0x00101000, 0x10101000, 0x00111000, 0x10111000, + 0x00101004, 0x10101004, 0x00111004, 0x10111004, + 0x20101000, 0x30101000, 0x20111000, 0x30111000, + 0x20101004, 0x30101004, 0x20111004, 0x30111004, + }, + { + /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ + 0x00000000, 0x08000000, 0x00000008, 0x08000008, + 0x00000400, 0x08000400, 0x00000408, 0x08000408, + 0x00020000, 0x08020000, 0x00020008, 0x08020008, + 0x00020400, 0x08020400, 0x00020408, 0x08020408, + 0x00000001, 0x08000001, 0x00000009, 0x08000009, + 0x00000401, 0x08000401, 0x00000409, 0x08000409, + 0x00020001, 0x08020001, 0x00020009, 0x08020009, + 0x00020401, 0x08020401, 0x00020409, 0x08020409, + 0x02000000, 0x0A000000, 0x02000008, 0x0A000008, + 0x02000400, 0x0A000400, 0x02000408, 0x0A000408, + 0x02020000, 0x0A020000, 0x02020008, 0x0A020008, + 0x02020400, 0x0A020400, 0x02020408, 0x0A020408, + 0x02000001, 0x0A000001, 0x02000009, 0x0A000009, + 0x02000401, 0x0A000401, 0x02000409, 0x0A000409, + 0x02020001, 0x0A020001, 0x02020009, 0x0A020009, + 0x02020401, 0x0A020401, 0x02020409, 0x0A020409, + }, + { + /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ + 0x00000000, 0x00000100, 0x00080000, 0x00080100, + 0x01000000, 0x01000100, 0x01080000, 0x01080100, + 0x00000010, 0x00000110, 0x00080010, 0x00080110, + 0x01000010, 0x01000110, 0x01080010, 0x01080110, + 0x00200000, 0x00200100, 0x00280000, 0x00280100, + 0x01200000, 0x01200100, 0x01280000, 0x01280100, + 0x00200010, 0x00200110, 0x00280010, 0x00280110, + 0x01200010, 0x01200110, 0x01280010, 0x01280110, + 0x00000200, 0x00000300, 0x00080200, 0x00080300, + 0x01000200, 0x01000300, 0x01080200, 0x01080300, + 0x00000210, 0x00000310, 0x00080210, 0x00080310, + 0x01000210, 0x01000310, 0x01080210, 0x01080310, + 0x00200200, 0x00200300, 0x00280200, 0x00280300, + 0x01200200, 0x01200300, 0x01280200, 0x01280300, + 0x00200210, 0x00200310, 0x00280210, 0x00280310, + 0x01200210, 0x01200310, 0x01280210, 0x01280310, + }, + { + /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ + 0x00000000, 0x04000000, 0x00040000, 0x04040000, + 0x00000002, 0x04000002, 0x00040002, 0x04040002, + 0x00002000, 0x04002000, 0x00042000, 0x04042000, + 0x00002002, 0x04002002, 0x00042002, 0x04042002, + 0x00000020, 0x04000020, 0x00040020, 0x04040020, + 0x00000022, 0x04000022, 0x00040022, 0x04040022, + 0x00002020, 0x04002020, 0x00042020, 0x04042020, + 0x00002022, 0x04002022, 0x00042022, 0x04042022, + 0x00000800, 0x04000800, 0x00040800, 0x04040800, + 0x00000802, 0x04000802, 0x00040802, 0x04040802, + 0x00002800, 0x04002800, 0x00042800, 0x04042800, + 0x00002802, 0x04002802, 0x00042802, 0x04042802, + 0x00000820, 0x04000820, 0x00040820, 0x04040820, + 0x00000822, 0x04000822, 0x00040822, 0x04040822, + 0x00002820, 0x04002820, 0x00042820, 0x04042820, + 0x00002822, 0x04002822, 0x00042822, 0x04042822, + }, + }; + + private static final int SPtrans[][] = + { + { + /* nibble 0 */ + 0x00820200, 0x00020000, 0x80800000, 0x80820200, + 0x00800000, 0x80020200, 0x80020000, 0x80800000, + 0x80020200, 0x00820200, 0x00820000, 0x80000200, + 0x80800200, 0x00800000, 0x00000000, 0x80020000, + 0x00020000, 0x80000000, 0x00800200, 0x00020200, + 0x80820200, 0x00820000, 0x80000200, 0x00800200, + 0x80000000, 0x00000200, 0x00020200, 0x80820000, + 0x00000200, 0x80800200, 0x80820000, 0x00000000, + 0x00000000, 0x80820200, 0x00800200, 0x80020000, + 0x00820200, 0x00020000, 0x80000200, 0x00800200, + 0x80820000, 0x00000200, 0x00020200, 0x80800000, + 0x80020200, 0x80000000, 0x80800000, 0x00820000, + 0x80820200, 0x00020200, 0x00820000, 0x80800200, + 0x00800000, 0x80000200, 0x80020000, 0x00000000, + 0x00020000, 0x00800000, 0x80800200, 0x00820200, + 0x80000000, 0x80820000, 0x00000200, 0x80020200, + }, + { + /* nibble 1 */ + 0x10042004, 0x00000000, 0x00042000, 0x10040000, + 0x10000004, 0x00002004, 0x10002000, 0x00042000, + 0x00002000, 0x10040004, 0x00000004, 0x10002000, + 0x00040004, 0x10042000, 0x10040000, 0x00000004, + 0x00040000, 0x10002004, 0x10040004, 0x00002000, + 0x00042004, 0x10000000, 0x00000000, 0x00040004, + 0x10002004, 0x00042004, 0x10042000, 0x10000004, + 0x10000000, 0x00040000, 0x00002004, 0x10042004, + 0x00040004, 0x10042000, 0x10002000, 0x00042004, + 0x10042004, 0x00040004, 0x10000004, 0x00000000, + 0x10000000, 0x00002004, 0x00040000, 0x10040004, + 0x00002000, 0x10000000, 0x00042004, 0x10002004, + 0x10042000, 0x00002000, 0x00000000, 0x10000004, + 0x00000004, 0x10042004, 0x00042000, 0x10040000, + 0x10040004, 0x00040000, 0x00002004, 0x10002000, + 0x10002004, 0x00000004, 0x10040000, 0x00042000, + }, + { + /* nibble 2 */ + 0x41000000, 0x01010040, 0x00000040, 0x41000040, + 0x40010000, 0x01000000, 0x41000040, 0x00010040, + 0x01000040, 0x00010000, 0x01010000, 0x40000000, + 0x41010040, 0x40000040, 0x40000000, 0x41010000, + 0x00000000, 0x40010000, 0x01010040, 0x00000040, + 0x40000040, 0x41010040, 0x00010000, 0x41000000, + 0x41010000, 0x01000040, 0x40010040, 0x01010000, + 0x00010040, 0x00000000, 0x01000000, 0x40010040, + 0x01010040, 0x00000040, 0x40000000, 0x00010000, + 0x40000040, 0x40010000, 0x01010000, 0x41000040, + 0x00000000, 0x01010040, 0x00010040, 0x41010000, + 0x40010000, 0x01000000, 0x41010040, 0x40000000, + 0x40010040, 0x41000000, 0x01000000, 0x41010040, + 0x00010000, 0x01000040, 0x41000040, 0x00010040, + 0x01000040, 0x00000000, 0x41010000, 0x40000040, + 0x41000000, 0x40010040, 0x00000040, 0x01010000, + }, + { + /* nibble 3 */ + 0x00100402, 0x04000400, 0x00000002, 0x04100402, + 0x00000000, 0x04100000, 0x04000402, 0x00100002, + 0x04100400, 0x04000002, 0x04000000, 0x00000402, + 0x04000002, 0x00100402, 0x00100000, 0x04000000, + 0x04100002, 0x00100400, 0x00000400, 0x00000002, + 0x00100400, 0x04000402, 0x04100000, 0x00000400, + 0x00000402, 0x00000000, 0x00100002, 0x04100400, + 0x04000400, 0x04100002, 0x04100402, 0x00100000, + 0x04100002, 0x00000402, 0x00100000, 0x04000002, + 0x00100400, 0x04000400, 0x00000002, 0x04100000, + 0x04000402, 0x00000000, 0x00000400, 0x00100002, + 0x00000000, 0x04100002, 0x04100400, 0x00000400, + 0x04000000, 0x04100402, 0x00100402, 0x00100000, + 0x04100402, 0x00000002, 0x04000400, 0x00100402, + 0x00100002, 0x00100400, 0x04100000, 0x04000402, + 0x00000402, 0x04000000, 0x04000002, 0x04100400, + }, + { + /* nibble 4 */ + 0x02000000, 0x00004000, 0x00000100, 0x02004108, + 0x02004008, 0x02000100, 0x00004108, 0x02004000, + 0x00004000, 0x00000008, 0x02000008, 0x00004100, + 0x02000108, 0x02004008, 0x02004100, 0x00000000, + 0x00004100, 0x02000000, 0x00004008, 0x00000108, + 0x02000100, 0x00004108, 0x00000000, 0x02000008, + 0x00000008, 0x02000108, 0x02004108, 0x00004008, + 0x02004000, 0x00000100, 0x00000108, 0x02004100, + 0x02004100, 0x02000108, 0x00004008, 0x02004000, + 0x00004000, 0x00000008, 0x02000008, 0x02000100, + 0x02000000, 0x00004100, 0x02004108, 0x00000000, + 0x00004108, 0x02000000, 0x00000100, 0x00004008, + 0x02000108, 0x00000100, 0x00000000, 0x02004108, + 0x02004008, 0x02004100, 0x00000108, 0x00004000, + 0x00004100, 0x02004008, 0x02000100, 0x00000108, + 0x00000008, 0x00004108, 0x02004000, 0x02000008, + }, + { + /* nibble 5 */ + 0x20000010, 0x00080010, 0x00000000, 0x20080800, + 0x00080010, 0x00000800, 0x20000810, 0x00080000, + 0x00000810, 0x20080810, 0x00080800, 0x20000000, + 0x20000800, 0x20000010, 0x20080000, 0x00080810, + 0x00080000, 0x20000810, 0x20080010, 0x00000000, + 0x00000800, 0x00000010, 0x20080800, 0x20080010, + 0x20080810, 0x20080000, 0x20000000, 0x00000810, + 0x00000010, 0x00080800, 0x00080810, 0x20000800, + 0x00000810, 0x20000000, 0x20000800, 0x00080810, + 0x20080800, 0x00080010, 0x00000000, 0x20000800, + 0x20000000, 0x00000800, 0x20080010, 0x00080000, + 0x00080010, 0x20080810, 0x00080800, 0x00000010, + 0x20080810, 0x00080800, 0x00080000, 0x20000810, + 0x20000010, 0x20080000, 0x00080810, 0x00000000, + 0x00000800, 0x20000010, 0x20000810, 0x20080800, + 0x20080000, 0x00000810, 0x00000010, 0x20080010, + }, + { + /* nibble 6 */ + 0x00001000, 0x00000080, 0x00400080, 0x00400001, + 0x00401081, 0x00001001, 0x00001080, 0x00000000, + 0x00400000, 0x00400081, 0x00000081, 0x00401000, + 0x00000001, 0x00401080, 0x00401000, 0x00000081, + 0x00400081, 0x00001000, 0x00001001, 0x00401081, + 0x00000000, 0x00400080, 0x00400001, 0x00001080, + 0x00401001, 0x00001081, 0x00401080, 0x00000001, + 0x00001081, 0x00401001, 0x00000080, 0x00400000, + 0x00001081, 0x00401000, 0x00401001, 0x00000081, + 0x00001000, 0x00000080, 0x00400000, 0x00401001, + 0x00400081, 0x00001081, 0x00001080, 0x00000000, + 0x00000080, 0x00400001, 0x00000001, 0x00400080, + 0x00000000, 0x00400081, 0x00400080, 0x00001080, + 0x00000081, 0x00001000, 0x00401081, 0x00400000, + 0x00401080, 0x00000001, 0x00001001, 0x00401081, + 0x00400001, 0x00401080, 0x00401000, 0x00001001, + }, + { + /* nibble 7 */ + 0x08200020, 0x08208000, 0x00008020, 0x00000000, + 0x08008000, 0x00200020, 0x08200000, 0x08208020, + 0x00000020, 0x08000000, 0x00208000, 0x00008020, + 0x00208020, 0x08008020, 0x08000020, 0x08200000, + 0x00008000, 0x00208020, 0x00200020, 0x08008000, + 0x08208020, 0x08000020, 0x00000000, 0x00208000, + 0x08000000, 0x00200000, 0x08008020, 0x08200020, + 0x00200000, 0x00008000, 0x08208000, 0x00000020, + 0x00200000, 0x00008000, 0x08000020, 0x08208020, + 0x00008020, 0x08000000, 0x00000000, 0x00208000, + 0x08200020, 0x08008020, 0x08008000, 0x00200020, + 0x08208000, 0x00000020, 0x00200020, 0x08008000, + 0x08208020, 0x00200000, 0x08200000, 0x08000020, + 0x00208000, 0x00008020, 0x08008020, 0x08200000, + 0x00000020, 0x08208000, 0x00208020, 0x00000000, + 0x08000000, 0x08200020, 0x00008000, 0x00208020 + } + }; + + private static final int cov_2char[] = + { + 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, + 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, + 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, + 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, + 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, + 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, + 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A + }; + + private static final int byteToUnsigned(byte b) + { + int value = (int)b; + + return(value >= 0 ? value : value + 256); + } + + private static int fourBytesToInt(byte b[], int offset) + { + int value; + + value = byteToUnsigned(b[offset++]); + value |= (byteToUnsigned(b[offset++]) << 8); + value |= (byteToUnsigned(b[offset++]) << 16); + value |= (byteToUnsigned(b[offset++]) << 24); + + return(value); + } + + private static final void intToFourBytes(int iValue, byte b[], int offset) + { + b[offset++] = (byte)((iValue) & 0xff); + b[offset++] = (byte)((iValue >>> 8 ) & 0xff); + b[offset++] = (byte)((iValue >>> 16) & 0xff); + b[offset++] = (byte)((iValue >>> 24) & 0xff); + } + + private static final void PERM_OP(int a, int b, int n, int m, int results[]) + { + int t; + + t = ((a >>> n) ^ b) & m; + a ^= t << n; + b ^= t; + + results[0] = a; + results[1] = b; + } + + private static final int HPERM_OP(int a, int n, int m) + { + int t; + + t = ((a << (16 - n)) ^ a) & m; + a = a ^ t ^ (t >>> (16 - n)); + + return(a); + } + + private static int [] des_set_key(byte key[]) + { + int schedule[] = new int[ITERATIONS * 2]; + + int c = fourBytesToInt(key, 0); + int d = fourBytesToInt(key, 4); + + int results[] = new int[2]; + + PERM_OP(d, c, 4, 0x0f0f0f0f, results); + d = results[0]; c = results[1]; + + c = HPERM_OP(c, -2, 0xcccc0000); + d = HPERM_OP(d, -2, 0xcccc0000); + + PERM_OP(d, c, 1, 0x55555555, results); + d = results[0]; c = results[1]; + + PERM_OP(c, d, 8, 0x00ff00ff, results); + c = results[0]; d = results[1]; + + PERM_OP(d, c, 1, 0x55555555, results); + d = results[0]; c = results[1]; + + d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) | + ((d & 0x00ff0000) >>> 16) | ((c & 0xf0000000) >>> 4)); + c &= 0x0fffffff; + + int s, t; + int j = 0; + + for(int i = 0; i < ITERATIONS; i ++) + { + if(shifts2[i]) + { + c = (c >>> 2) | (c << 26); + d = (d >>> 2) | (d << 26); + } + else + { + c = (c >>> 1) | (c << 27); + d = (d >>> 1) | (d << 27); + } + + c &= 0x0fffffff; + d &= 0x0fffffff; + + s = skb[0][ (c ) & 0x3f ]| + skb[1][((c >>> 6) & 0x03) | ((c >>> 7) & 0x3c)]| + skb[2][((c >>> 13) & 0x0f) | ((c >>> 14) & 0x30)]| + skb[3][((c >>> 20) & 0x01) | ((c >>> 21) & 0x06) | + ((c >>> 22) & 0x38)]; + + t = skb[4][ (d ) & 0x3f ]| + skb[5][((d >>> 7) & 0x03) | ((d >>> 8) & 0x3c)]| + skb[6][ (d >>>15) & 0x3f ]| + skb[7][((d >>>21) & 0x0f) | ((d >>> 22) & 0x30)]; + + schedule[j++] = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff; + s = ((s >>> 16) | (t & 0xffff0000)); + + s = (s << 4) | (s >>> 28); + schedule[j++] = s & 0xffffffff; + } + return(schedule); + } + + private static final int D_ENCRYPT + ( + int L, int R, int S, int E0, int E1, int s[] + ) + { + int t, u, v; + + v = R ^ (R >>> 16); + u = v & E0; + v = v & E1; + u = (u ^ (u << 16)) ^ R ^ s[S]; + t = (v ^ (v << 16)) ^ R ^ s[S + 1]; + t = (t >>> 4) | (t << 28); + + L ^= SPtrans[1][(t ) & 0x3f] | + SPtrans[3][(t >>> 8) & 0x3f] | + SPtrans[5][(t >>> 16) & 0x3f] | + SPtrans[7][(t >>> 24) & 0x3f] | + SPtrans[0][(u ) & 0x3f] | + SPtrans[2][(u >>> 8) & 0x3f] | + SPtrans[4][(u >>> 16) & 0x3f] | + SPtrans[6][(u >>> 24) & 0x3f]; + + return(L); + } + + private static final int [] body(int schedule[], int Eswap0, int Eswap1) + { + int left = 0; + int right = 0; + int t = 0; + + for(int j = 0; j < 25; j ++) + { + for(int i = 0; i < ITERATIONS * 2; i += 4) + { + left = D_ENCRYPT(left, right, i, Eswap0, Eswap1, schedule); + right = D_ENCRYPT(right, left, i + 2, Eswap0, Eswap1, schedule); + } + t = left; + left = right; + right = t; + } + + t = right; + + right = (left >>> 1) | (left << 31); + left = (t >>> 1) | (t << 31); + + left &= 0xffffffff; + right &= 0xffffffff; + + int results[] = new int[2]; + + PERM_OP(right, left, 1, 0x55555555, results); + right = results[0]; left = results[1]; + + PERM_OP(left, right, 8, 0x00ff00ff, results); + left = results[0]; right = results[1]; + + PERM_OP(right, left, 2, 0x33333333, results); + right = results[0]; left = results[1]; + + PERM_OP(left, right, 16, 0x0000ffff, results); + left = results[0]; right = results[1]; + + PERM_OP(right, left, 4, 0x0f0f0f0f, results); + right = results[0]; left = results[1]; + + int out[] = new int[2]; + + out[0] = left; out[1] = right; + + return(out); + } + + /** + * <P>Encrypt a password given the cleartext password and a "salt".</P> + * @param salt A two-character string representing the salt used to + * iterate the encryption engine in lots of different ways. If you + * are generating a new encryption then this value should be + * randomised. + * @param original The password to be encrypted. + * @return A string consisting of the 2-character salt followed by the + * encrypted password. + */ + public static final String crypt(String salt, String original) + { + while(salt.length() < 2) + salt += "A"; + + StringBuffer buffer = new StringBuffer(" "); + + char charZero = salt.charAt(0); + char charOne = salt.charAt(1); + + buffer.setCharAt(0, charZero); + buffer.setCharAt(1, charOne); + + int Eswap0 = con_salt[(int)charZero]; + int Eswap1 = con_salt[(int)charOne] << 4; + + byte key[] = new byte[8]; + + for(int i = 0; i < key.length; i ++) + key[i] = (byte)0; + + for(int i = 0; i < key.length && i < original.length(); i ++) + { + int iChar = (int)original.charAt(i); + + key[i] = (byte)(iChar << 1); + } + + int schedule[] = des_set_key(key); + int out[] = body(schedule, Eswap0, Eswap1); + + byte b[] = new byte[9]; + + intToFourBytes(out[0], b, 0); + intToFourBytes(out[1], b, 4); + b[8] = 0; + + for(int i = 2, y = 0, u = 0x80; i < 13; i ++) + { + for(int j = 0, c = 0; j < 6; j ++) + { + c <<= 1; + + if(((int)b[y] & u) != 0) + c |= 1; + + u >>>= 1; + + if(u == 0) + { + y++; + u = 0x80; + } + buffer.setCharAt(i, (char)cov_2char[c]); + } + } + return(buffer.toString()); + } + + /** + * <P>Encrypt a password given the cleartext password. This method + * generates a random salt using the 'java.util.Random' class.</P> + * @param original The password to be encrypted. + * @return A string consisting of the 2-character salt followed by the + * encrypted password. + */ + public static final String crypt(String original) + { + java.util.Random randomGenerator = new java.util.Random(); + int numSaltChars = saltChars.length; + String salt; + + salt = (new StringBuffer()).append(saltChars[Math.abs(randomGenerator.nextInt()) % numSaltChars]).append(saltChars[Math.abs(randomGenerator.nextInt()) % numSaltChars]).toString(); + + return crypt(salt, original); + } + + /** + * <P>Check that <I>enteredPassword</I> encrypts to + * <I>encryptedPassword</I>.</P> + * @param encryptedPassword The <I>encryptedPassword</I>. The first + * two characters are assumed to be the salt. This string would + * be the same as one found in a Unix <U>/etc/passwd</U> file. + * @param enteredPassword The password as entered by the user (or + * otherwise aquired). + * @return <B>true</B> if the password should be considered correct. + */ + public final static boolean matches(String encryptedPassword, String enteredPassword) + { + String salt = encryptedPassword.substring(0, 3); + String newCrypt = crypt(salt, enteredPassword); + + return newCrypt.equals(encryptedPassword); + } + + public static void main(String args[]) { + String encryptedPassword = "$6$BFpYzdUp$RH1M4rlc/.dXSsNJQTklT9z8lKQFwRYh7sSoFTFFgqdC596gB4Kvzh/ZlusL0EU6E9tpW8SnDOYlqPph8NBBP."; + String salt = encryptedPassword.substring(0, 3); + String newCrypt = crypt("BF", "gluster"); + System.out.println(newCrypt); + + + System.out.println("$6$BFpYzdUp$RH1M4rlc/.dXSsNJQTklT9z8lKQFwRYh7sSoFTFFgqdC596gB4Kvzh/ZlusL0EU6E9tpW8SnDOYlqPph8NBBP."); + } +} diff --git a/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/sample_jaas.config b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/sample_jaas.config new file mode 100644 index 00000000..c9e3878c --- /dev/null +++ b/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/sample_jaas.config @@ -0,0 +1,3 @@ +Sample { + sample.module.SampleLoginModule required debug=true; +}; |
