/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.admin.KerberosAdmin
 *  com.l7tech.gateway.common.cluster.ClusterProperty
 *  com.l7tech.kerberos.KerberosClient
 *  com.l7tech.kerberos.KerberosConfigException
 *  com.l7tech.kerberos.KerberosException
 *  com.l7tech.kerberos.KerberosUtils
 *  com.l7tech.kerberos.KeyTabEntryInfo
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  com.l7tech.util.MasterPasswordManager
 */
package com.l7tech.server;

import com.l7tech.gateway.common.admin.KerberosAdmin;
import com.l7tech.gateway.common.cluster.ClusterProperty;
import com.l7tech.kerberos.KerberosClient;
import com.l7tech.kerberos.KerberosConfigException;
import com.l7tech.kerberos.KerberosException;
import com.l7tech.kerberos.KerberosUtils;
import com.l7tech.kerberos.KeyTabEntryInfo;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.server.ap;
import com.l7tech.server.cluster.ClusterPropertyManager;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import com.l7tech.util.MasterPasswordManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.security.krb5.internal.ktab.KeyTab;
import sun.security.krb5.internal.ktab.KeyTabEntry;

public class KerberosAdminImpl
implements KerberosAdmin {
    private static final Logger a;
    private static final String b;
    private static final String c;
    private final ClusterPropertyManager d;
    private final MasterPasswordManager e;
    private static final String[] f;

    public KerberosAdminImpl(ClusterPropertyManager clusterPropertyManager, MasterPasswordManager masterPasswordManager) {
        this.d = clusterPropertyManager;
        this.e = masterPasswordManager;
    }

    public List<KeyTabEntryInfo> getKeyTabEntryInfos() throws KerberosException {
        ArrayList<KeyTabEntryInfo> arrayList = new ArrayList<KeyTabEntryInfo>();
        try {
            KeyTab keyTab = KerberosClient.getKerberosAcceptPrincipalKeytab();
            if (keyTab != null) {
                KeyTabEntry[] keyTabEntryArray = keyTab.getEntries();
                for (int i2 = 0; i2 < keyTabEntryArray.length; ++i2) {
                    Date date;
                    KeyTabEntryInfo keyTabEntryInfo;
                    KeyTabEntry keyTabEntry = keyTabEntryArray[i2];
                    KeyTabEntryInfo keyTabEntryInfo2 = new KeyTabEntryInfo();
                    try {
                        keyTabEntryInfo2.setRealm(keyTabEntry.getService().getRealm().toString());
                        keyTabEntryInfo2.setKdc(KerberosUtils.getKdc((String)keyTabEntryInfo2.getRealm()));
                        keyTabEntryInfo2.setPrincipalName(keyTabEntry.getService().getName());
                        keyTabEntryInfo = keyTabEntryInfo2;
                        date = keyTabEntry.getTimeStamp() == null ? null : keyTabEntry.getTimeStamp().toDate();
                    }
                    catch (KerberosException kerberosException) {
                        throw KerberosAdminImpl.a((Exception)((Object)kerberosException));
                    }
                    keyTabEntryInfo.setDate(date);
                    keyTabEntryInfo2.setVersion(keyTabEntry.getKey().getKeyVersionNumber());
                    keyTabEntryInfo2.setEtype(keyTabEntry.getKey().getEType());
                    arrayList.add(keyTabEntryInfo2);
                }
            }
        }
        catch (KerberosException kerberosException) {
            a.log(Level.WARNING, f[10], kerberosException);
            throw new KerberosException(kerberosException.getMessage());
        }
        return arrayList;
    }

    public void validatePrincipal(String string) throws KerberosException {
        KerberosException[] kerberosExceptionArray = new KerberosException[1];
        String[] stringArray = new String[1];
        String[] stringArray2 = f;
        Thread thread = new Thread((Runnable)new ap(this, stringArray, string, kerberosExceptionArray), stringArray2[6]);
        thread.start();
        try {
            thread.join(30000L);
        }
        catch (InterruptedException interruptedException) {
            stringArray2 = f;
            throw new KerberosConfigException(stringArray2[5] + ExceptionUtils.getMessage((Throwable)interruptedException) + stringArray2[1], (Throwable)interruptedException);
        }
        try {
            if (thread.isAlive()) {
                a.log(Level.WARNING, f[3] + string);
                throw new KerberosException(f[13]);
            }
        }
        catch (InterruptedException interruptedException) {
            throw KerberosAdminImpl.a(interruptedException);
        }
        try {
            if (kerberosExceptionArray[0] != null) {
                throw kerberosExceptionArray[0];
            }
        }
        catch (InterruptedException interruptedException) {
            throw KerberosAdminImpl.a(interruptedException);
        }
    }

    public Map<String, String> getConfiguration() {
        new KerberosClient();
        HashMap<String, String> hashMap = new HashMap<String, String>();
        String string = KerberosUtils.getKerberosKdc();
        String string2 = KerberosUtils.getKerberosRealm();
        if (string != null) {
            String[] stringArray = f;
            hashMap.put(stringArray[8], string);
        }
        if (string2 != null) {
            hashMap.put(f[12], string2);
        }
        return Collections.unmodifiableMap(hashMap);
    }

    public void installKeytab(byte[] byArray) throws KerberosException {
        block6: {
            try {
                ClusterProperty clusterProperty;
                block5: {
                    String[] stringArray = f;
                    clusterProperty = (ClusterProperty)this.d.findByUniqueName(stringArray[0]);
                    if (clusterProperty == null) {
                        clusterProperty = new ClusterProperty();
                        clusterProperty.setName(stringArray[16]);
                    }
                    clusterProperty.setValue(this.e.encryptPassword(HexUtils.encodeBase64((byte[])byArray).toCharArray()));
                    Goid goid = clusterProperty.getGoid();
                    try {
                        if (!ClusterProperty.DEFAULT_GOID.equals((Object)goid)) break block5;
                        this.d.save((PersistentEntity)clusterProperty);
                        break block6;
                    }
                    catch (ObjectModelException objectModelException) {
                        throw KerberosAdminImpl.a((Exception)((Object)objectModelException));
                    }
                }
                this.d.update((PersistentEntity)clusterProperty);
            }
            catch (ObjectModelException objectModelException) {
                objectModelException.printStackTrace();
                throw new KerberosException(f[9], (Throwable)objectModelException);
            }
        }
    }

    public void deleteKeytab() throws KerberosException {
        try {
            String[] stringArray = f;
            ClusterProperty clusterProperty = (ClusterProperty)this.d.findByUniqueName(stringArray[16]);
            try {
                if (clusterProperty == null) {
                    throw new KerberosException(stringArray[14]);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw KerberosAdminImpl.a((Exception)((Object)objectModelException));
            }
            this.d.delete((PersistentEntity)clusterProperty);
        }
        catch (ObjectModelException objectModelException) {
            objectModelException.printStackTrace();
            throw new KerberosException(f[2], (Throwable)objectModelException);
        }
    }

    public void setKeytabValidate(boolean bl2) throws KerberosException {
        block6: {
            try {
                ClusterProperty clusterProperty;
                block5: {
                    String[] stringArray = f;
                    clusterProperty = (ClusterProperty)this.d.findByUniqueName(stringArray[4]);
                    if (clusterProperty == null) {
                        clusterProperty = new ClusterProperty();
                        clusterProperty.setName(stringArray[7]);
                    }
                    clusterProperty.setValue(Boolean.toString(bl2));
                    Goid goid = clusterProperty.getGoid();
                    try {
                        if (!ClusterProperty.DEFAULT_GOID.equals((Object)goid)) break block5;
                        this.d.save((PersistentEntity)clusterProperty);
                        break block6;
                    }
                    catch (ObjectModelException objectModelException) {
                        throw KerberosAdminImpl.a((Exception)((Object)objectModelException));
                    }
                }
                this.d.update((PersistentEntity)clusterProperty);
            }
            catch (ObjectModelException objectModelException) {
                throw new KerberosException(f[15], (Throwable)objectModelException);
            }
        }
    }

    public boolean getKeytabValidate() throws KerberosException {
        try {
            String[] stringArray = f;
            ClusterProperty clusterProperty = (ClusterProperty)this.d.findByUniqueName(stringArray[7]);
            if (clusterProperty == null) {
                return true;
            }
            return Boolean.parseBoolean(clusterProperty.getValue());
        }
        catch (ObjectModelException objectModelException) {
            throw new KerberosException(f[11], (Throwable)objectModelException);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[17];
                var3_1 = 0;
                var2_2 = "\u000ecK\bjdZ\u001ceH_\u0002B?\u0016 c[R6/[\u0000}LI-aXEzLD0n]K#.t[_!}P\u00161HH0gZ\u000be@^%{V\n\u007f\tI-bZ\u00011FH05\u001f\u0014\u000ecK\bjdZ\u001ceH_jy^\txM\\0j\u001e.t[_!}P\u00161JR*iV\u0002d[\\0fP\u000b1LO6`ME6\u001b.t[_!}P\u0016RFS\"fX\u0010cHI-`Q1tZI!}\u0014\u000ecK\bjdZ\u001ceH_jy^\txM\\0j\u0003\u000euJ\u0018 c[R6/O\u0000cZT7{V\u000bv\tV!vK\u0004s\u0007\u001a.t[_!}P\u00161BX={^\u00071@NdfQ\u0013pET % c[R6/M\u0000e[T!yV\u000bv\tV6m\nKzLD0n]KgHQ-k^\u0011t\u0005\u0017tHQ)8.t[_!}P\u00161JR*iV\u0002d[\\0fP\u000b1LO6`ME6hH0gZ\u000be@^%{V\n\u007f\tI-bZ\u00011FH0(\u0011\u0015!tEX0fP\u000b1GR0/O\nbZT&cZ";
                var4_3 = "\u000ecK\bjdZ\u001ceH_\u0002B?\u0016 c[R6/[\u0000}LI-aXEzLD0n]K#.t[_!}P\u00161HH0gZ\u000be@^%{V\n\u007f\tI-bZ\u00011FH05\u001f\u0014\u000ecK\bjdZ\u001ceH_jy^\txM\\0j\u001e.t[_!}P\u00161JR*iV\u0002d[\\0fP\u000b1LO6`ME6\u001b.t[_!}P\u0016RFS\"fX\u0010cHI-`Q1tZI!}\u0014\u000ecK\bjdZ\u001ceH_jy^\txM\\0j\u0003\u000euJ\u0018 c[R6/O\u0000cZT7{V\u000bv\tV!vK\u0004s\u0007\u001a.t[_!}P\u00161BX={^\u00071@NdfQ\u0013pET % c[R6/M\u0000e[T!yV\u000bv\tV6m\nKzLD0n]KgHQ-k^\u0011t\u0005\u0017tHQ)8.t[_!}P\u00161JR*iV\u0002d[\\0fP\u000b1LO6`ME6hH0gZ\u000be@^%{V\n\u007f\tI-bZ\u00011FH0(\u0011\u0015!tEX0fP\u000b1GR0/O\nbZT&cZ".length();
                var1_4 = 11;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 40;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = KerberosAdminImpl.a(v0, KerberosAdminImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "Y\u001a\"+OV6y\u001a#-N\u0002/r\u000fp/O\u0014s2\u00035=I\u0017$2\u001e1(T\u0012'h\r\u000bw\u001a2q\u0013\u001d#e\u001c1&";
                    var4_3 = "Y\u001a\"+OV6y\u001a#-N\u0002/r\u000fp/O\u0014s2\u00035=I\u0017$2\u001e1(T\u0012'h\r\u000bw\u001a2q\u0013\u001d#e\u001c1&".length();
                    var1_4 = 37;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 81;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = KerberosAdminImpl.a(v0, KerberosAdminImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        KerberosAdminImpl.f = var5;
        KerberosAdminImpl.b = KerberosAdminImpl.f[16];
        KerberosAdminImpl.c = KerberosAdminImpl.f[7];
        KerberosAdminImpl.a = Logger.getLogger(KerberosAdminImpl.class.getName());
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x17);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 77;
                    case 1 -> 57;
                    case 2 -> 1;
                    case 3 -> 21;
                    case 4 -> 108;
                    case 5 -> 39;
                    default -> 23;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

