]> WPIA git - gigi.git/commitdiff
Merge "upd: don’t initialize name.suffix from name.www"
authorBenny Baumann <BenBE1987@gmx.net>
Wed, 22 Mar 2017 21:05:36 +0000 (22:05 +0100)
committerGerrit Code Review <gigi-system@dogcraft.de>
Wed, 22 Mar 2017 21:05:36 +0000 (22:05 +0100)
natives/README.md [new file with mode: 0644]
src/club/wpia/gigi/util/DNSUtil.java
src/club/wpia/gigi/util/DomainAssessment.java
src/club/wpia/gigi/util/PasswordHash.java
tests/club/wpia/gigi/util/TestPasswordMigration.java

diff --git a/natives/README.md b/natives/README.md
new file mode 100644 (file)
index 0000000..49144df
--- /dev/null
@@ -0,0 +1,10 @@
+This native method exposes the *man:setuid(2)* and *man:setgid(2)* system calls to Java.
+Java code can call `club.wpia.gigi.natives.SetUID.setUid(uid, gid)` to set the user and group ID to the specified values if they’re currently different.
+
+Gigi can use this to bind to Internet domain privileged ports (port numbers below 1024)
+when started as root and then drop privileges by changing to a non-root user.
+
+It should be noted that this is rarely necessary;
+it is much safer to start Gigi as a regular user with `CAP_NET_BIND_SERVICE` (see *man:capabilities(7)*).
+Gigi can also inherit its socket from the environment (file descriptor 0),
+e. g. from systemd (see *man:systemd.socket(5)*) or (x)inetd.
index af664359b68bf71ed6cfe78f39be9e7a7e019176..64a2096461d5d0ce1c322eddc10a0e47f0bc6712 100644 (file)
@@ -38,17 +38,17 @@ public class DNSUtil {
         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
         env.put(Context.AUTHORITATIVE, "true");
         env.put(Context.PROVIDER_URL, "dns://" + server);
+
         InitialDirContext context = new InitialDirContext(env);
         try {
-
             Attributes dnsLookup = context.getAttributes(name, new String[] {
                     "TXT"
             });
+
             return extractTextEntries(dnsLookup.get("TXT"));
         } finally {
             context.close();
         }
-
     }
 
     private static String[] extractTextEntries(Attribute nsRecords) throws NamingException {
@@ -72,27 +72,35 @@ public class DNSUtil {
     public static CAARecord[] getCAAEntries(String domain) throws NamingException {
         Hashtable<String, String> env = new Hashtable<String, String>();
         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
+
         InitialDirContext context = new InitialDirContext(env);
-        Attributes dnsLookup;
         try {
-            dnsLookup = context.getAttributes(domain, new String[] {
+            Attributes dnsLookup;
+            try {
+                dnsLookup = context.getAttributes(domain, new String[] {
                     "257"
-            });
-        } catch (NameNotFoundException e) {
-            // We treat non-existing names as names without CAA-records
-            return new CAARecord[0];
-        }
-        Attribute nsRecords = dnsLookup.get("257");
-        if (nsRecords == null) {
-            return new CAARecord[] {};
-        }
-        CAA.CAARecord[] result = new CAA.CAARecord[nsRecords.size()];
-        for (int i = 0; i < result.length; i++) {
-            byte[] rec = (byte[]) nsRecords.get(i);
+                });
+            } catch (NameNotFoundException e) {
+                // We treat non-existing names as names without CAA-records
+                return new CAARecord[0];
+            }
+
+            Attribute nsRecords = dnsLookup.get("257");
+            if (nsRecords == null) {
+                return new CAARecord[] {};
+            }
+
+            CAA.CAARecord[] result = new CAA.CAARecord[nsRecords.size()];
+            for (int i = 0; i < result.length; i++) {
+                byte[] rec = (byte[]) nsRecords.get(i);
 
-            result[i] = new CAA.CAARecord(rec);
+                result[i] = new CAA.CAARecord(rec);
+            }
+
+            return result;
+        } finally {
+            context.close();
         }
-        return result;
     }
 
     public static void main(String[] args) throws NamingException {
index d24404ed4676752aa9aab075c2186c43a2c9f400..e17f2eea2ae724173c5f4f6846996690077a47eb 100644 (file)
@@ -148,11 +148,14 @@ public class DomainAssessment {
 
     public static void init(Properties conf) {
         String financialName = conf.getProperty("highFinancialValue");
+
         if (financialName == null) {
             throw new Error("No property highFinancialValue was configured");
         }
-        try {
-            financial = new DomainSet(new InputStreamReader(new FileInputStream(new File(financialName)), "UTF-8"));
+
+        try (FileInputStream fis = new FileInputStream(new File(financialName)); //
+                InputStreamReader isr = new InputStreamReader(fis, "UTF-8")) {
+            financial = new DomainSet(isr);
         } catch (IOException e) {
             throw new Error(e);
         }
index 51dffb70d8ed0763548ce7be6fed104364682c30..7d2cb3342e92b0d4f6921c54b2191ce783c6083e 100644 (file)
@@ -1,8 +1,5 @@
 package club.wpia.gigi.util;
 
-import java.io.UnsupportedEncodingException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.Properties;
 
 import com.lambdaworks.crypto.SCryptUtil;
@@ -29,6 +26,7 @@ public class PasswordHash {
         if (password == null || password.isEmpty()) {
             return null;
         }
+
         if (hash.contains("$")) {
             if (SCryptUtil.check(password, hash)) {
                 return hash;
@@ -36,36 +34,8 @@ public class PasswordHash {
                 return null;
             }
         }
-        String newhash = sha1(password);
-        boolean match = true;
-        if (newhash.length() != hash.length()) {
-            match = false;
-        }
-        for (int i = 0; i < newhash.length(); i++) {
-            match &= newhash.charAt(i) == hash.charAt(i);
-        }
-        if (match) {
-            return hash(password);
-        } else {
-            return null;
-        }
-    }
 
-    public static String sha1(String password) {
-        try {
-            MessageDigest md = MessageDigest.getInstance("SHA1");
-            byte[] digest = md.digest(password.getBytes("UTF-8"));
-            StringBuffer res = new StringBuffer(digest.length * 2);
-            for (int i = 0; i < digest.length; i++) {
-                res.append(Integer.toHexString((digest[i] & 0xF0) >> 4));
-                res.append(Integer.toHexString(digest[i] & 0xF));
-            }
-            return res.toString();
-        } catch (NoSuchAlgorithmException e) {
-            throw new Error(e);
-        } catch (UnsupportedEncodingException e) {
-            throw new Error(e);
-        }
+        return null;
     }
 
     public static String hash(String password) {
index 17544a0bdc610737aa0af8be9771f3c5f2de614d..1ad8ae8a04a55ad829a1f8b78bc7509a35dc0575 100644 (file)
@@ -12,29 +12,39 @@ import club.wpia.gigi.database.GigiPreparedStatement;
 import club.wpia.gigi.database.GigiResultSet;
 import club.wpia.gigi.testUtils.ManagedTest;
 import club.wpia.gigi.testUtils.RegisteredUser;
-import club.wpia.gigi.util.PasswordHash;
 
 public class TestPasswordMigration extends ManagedTest {
 
     @Rule
     public RegisteredUser ru = new RegisteredUser();
 
+    /**
+     * Gigi used to support plain SHA-1 password hashes, for compatibility with
+     * legacy software. Since there currently is only one accepted hash format,
+     * this test now verifies that plain SHA-1 hashes are no longer accepted nor
+     * migrated to more recent hash formats.
+     *
+     * @see PasswordHash.verifyHash
+     * @see PasswordHash.hash
+     * @throws IOException
+     */
     @Test
-    public void testPasswordMigration() throws IOException {
+    public void testNoSHA1PasswordMigration() throws IOException {
         try (GigiPreparedStatement stmt = new GigiPreparedStatement("UPDATE users SET `password`=? WHERE id=?")) {
-            stmt.setString(1, PasswordHash.sha1("a"));
+            stmt.setString(1, "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"); // sha1("a")
             stmt.setInt(2, ru.getUser().getId());
             stmt.execute();
         }
+
         String cookie = login(ru.getUser().getEmail(), "a");
-        assertTrue(isLoggedin(cookie));
+        assertFalse(isLoggedin(cookie));
 
         try (GigiPreparedStatement stmt = new GigiPreparedStatement("SELECT `password` FROM users WHERE id=?")) {
             stmt.setInt(1, ru.getUser().getId());
             GigiResultSet res = stmt.executeQuery();
             assertTrue(res.next());
             String newHash = res.getString(1);
-            assertThat(newHash, containsString("$"));
+            assertThat(newHash, not(containsString("$")));
         }
     }
 }