|
|
@ -1,5 +1,6 @@ |
|
|
|
package de.hsfulda.informatik; |
|
|
|
|
|
|
|
import com.google.common.base.Strings; |
|
|
|
import com.unboundid.ldap.sdk.Attribute; |
|
|
|
import com.unboundid.ldap.sdk.Entry; |
|
|
|
import com.unboundid.ldap.sdk.LDAPException; |
|
|
@ -8,9 +9,12 @@ import java.io.FileNotFoundException; |
|
|
|
import java.io.FileReader; |
|
|
|
import java.io.IOException; |
|
|
|
import java.security.GeneralSecurityException; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.TreeMap; |
|
|
|
import java.util.TreeSet; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
@ -20,14 +24,17 @@ import java.util.stream.Collectors; |
|
|
|
* Ldap sync utility |
|
|
|
*/ |
|
|
|
public class LdapSync { |
|
|
|
final Properties properties = new Properties(); |
|
|
|
final static Pattern pattern = Pattern.compile("^fd([a-z][a-z])?([0-9]*?)$"); |
|
|
|
private final Properties properties = new Properties(); |
|
|
|
private final static Pattern pattern = Pattern.compile("^fd([a-z][a-z])?([0-9]*?)$"); |
|
|
|
private Map<String, Integer> userDefinedMappings = Collections.emptyMap(); |
|
|
|
|
|
|
|
public LdapSync() throws IOException, LDAPException, GeneralSecurityException { |
|
|
|
// lade Konfiguration |
|
|
|
properties.load(new FileReader("ldap-sync.properties")); |
|
|
|
System.out.print("Abfrage der Benutzer im eDirectory..."); |
|
|
|
|
|
|
|
this.userDefinedMappings = parseUserDefinedMappings(properties.getProperty("sync.src.map", "")); |
|
|
|
|
|
|
|
// lade Daten des Remote-Systems |
|
|
|
final AccountSource remote = new AccountSource( |
|
|
|
properties.getProperty("sync.src.host"), |
|
|
@ -57,6 +64,37 @@ public class LdapSync { |
|
|
|
sync(remote, local); |
|
|
|
} |
|
|
|
|
|
|
|
static Map<String, Integer> parseUserDefinedMappings(final String property) { |
|
|
|
if (Strings.isNullOrEmpty(property)) { |
|
|
|
return Collections.emptyMap(); |
|
|
|
} |
|
|
|
|
|
|
|
final Map<String, Integer> mappingsMap = new TreeMap<>(); |
|
|
|
final String mappingArr[] = property.split("\\s*,\\s*"); |
|
|
|
|
|
|
|
for (final String mapping : mappingArr) { |
|
|
|
final String accountUidArr[] = mapping.split(":"); |
|
|
|
if (accountUidArr.length == 2) { |
|
|
|
final String uid = accountUidArr[0].toLowerCase(); |
|
|
|
final int uidNumber; |
|
|
|
|
|
|
|
try { |
|
|
|
uidNumber = Integer.valueOf(accountUidArr[1]); |
|
|
|
} catch (NumberFormatException e) { |
|
|
|
System.out.println("Fehler beim Parsen der uidNumber für uid '" + uid + "'"); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
System.out.println("Benutzerdefinierte Zuordnung '" + uid + "' -> " + uidNumber); |
|
|
|
mappingsMap.put(uid, uidNumber); |
|
|
|
} else { |
|
|
|
System.out.println("Fehler beim Parsen der Zuordnung '" + mapping + "'"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return mappingsMap; |
|
|
|
} |
|
|
|
|
|
|
|
LdapSync(final AccountSource remote, final AccountSource local) throws IOException, LDAPException, GeneralSecurityException { |
|
|
|
sync(remote, local); |
|
|
|
} |
|
|
@ -82,7 +120,7 @@ public class LdapSync { |
|
|
|
e.addAttribute(new Attribute("sn", s.getAttributeValue("sn"))); |
|
|
|
e.addAttribute(new Attribute("givenname", s.getAttributeValue("givenName"))); |
|
|
|
e.addAttribute(new Attribute("uid", cn)); |
|
|
|
e.addAttribute(new Attribute("uidNumber", String.valueOf(computeUid(cn)))); |
|
|
|
e.addAttribute(new Attribute("uidNumber", String.valueOf(computeUid(cn, this.userDefinedMappings)))); |
|
|
|
e.addAttribute(new Attribute("gidNumber", "20")); |
|
|
|
e.addAttribute(new Attribute("loginShell", "/bin/zsh")); |
|
|
|
e.addAttribute(new Attribute("homeDirectory", "/Users/" + cn)); |
|
|
@ -98,6 +136,14 @@ public class LdapSync { |
|
|
|
} |
|
|
|
|
|
|
|
static Integer computeUid(final String cn) { |
|
|
|
return computeUid(cn, Collections.emptyMap()); |
|
|
|
} |
|
|
|
|
|
|
|
static Integer computeUid(final String cn, final Map<String, Integer> mappings) { |
|
|
|
if (mappings.containsKey(cn)) { |
|
|
|
return mappings.get(cn); |
|
|
|
} |
|
|
|
|
|
|
|
final Matcher m = pattern.matcher(cn); |
|
|
|
|
|
|
|
if (m.find()) { |
|
|
|