X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FDomain.java;h=d4ffd8e28d377b038953a1cc2e2d808aeb16fd05;hp=056af154173b7ae9616c1f7c0b4eb07528997e4f;hb=36658ae4a12bea7095e02e45b1fe8400d85add8b;hpb=e409ba881965634f63f0b67824bc93dda4ec4327 diff --git a/src/org/cacert/gigi/dbObjects/Domain.java b/src/org/cacert/gigi/dbObjects/Domain.java index 056af154..d4ffd8e2 100644 --- a/src/org/cacert/gigi/dbObjects/Domain.java +++ b/src/org/cacert/gigi/dbObjects/Domain.java @@ -1,93 +1,85 @@ package org.cacert.gigi.dbObjects; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; import org.cacert.gigi.GigiApiException; -import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.util.DomainAssessment; -public class Domain implements IdCachable { +public class Domain implements IdCachable, Verifyable { - private User owner; + private CertificateOwner owner; private String suffix; private int id; - public Domain(int id) throws SQLException { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL"); - ps.setInt(1, id); - - ResultSet rs = ps.executeQuery(); - if ( !rs.next()) { - throw new IllegalArgumentException("Invalid email id " + id); - } + private Domain(GigiResultSet rs, int id) { this.id = id; - owner = User.getById(rs.getInt(1)); + owner = CertificateOwner.getById(rs.getInt(1)); suffix = rs.getString(2); - rs.close(); } - public Domain(User owner, String suffix) throws GigiApiException { - this.owner = owner; - this.suffix = suffix; - + public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException { + suffix = suffix.toLowerCase(); + synchronized (Domain.class) { + DomainAssessment.checkCertifiableDomain(suffix, actor.isInGroup(Group.CODESIGNING), true); + this.owner = owner; + this.suffix = suffix; + insert(); + } } private static void checkInsert(String suffix) throws GigiApiException { - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=RIGHT(?,LENGTH(domain)) OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL"); + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domains` WHERE (`domain`=? OR (CONCAT('.', `domain`)=RIGHT(?,LENGTH(`domain`)+1) OR RIGHT(`domain`,LENGTH(?)+1)=CONCAT('.',?))) AND `deleted` IS NULL")) { ps.setString(1, suffix); ps.setString(2, suffix); ps.setString(3, suffix); - ResultSet rs = ps.executeQuery(); + ps.setString(4, suffix); + GigiResultSet rs = ps.executeQuery(); boolean existed = rs.next(); rs.close(); if (existed) { - throw new GigiApiException("Domain could not be inserted. Domain is already valid."); + throw new GigiApiException("Domain could not be inserted. Domain is already known to the system."); } - } catch (SQLException e) { - throw new GigiApiException(e); } } - public void insert() throws GigiApiException { + private void insert() throws GigiApiException { if (id != 0) { throw new GigiApiException("already inserted."); } - synchronized (Domain.class) { - checkInsert(suffix); - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?"); - ps.setInt(1, owner.getId()); - ps.setString(2, suffix); - ps.execute(); - id = DatabaseConnection.lastInsertId(ps); - myCache.put(this); - } catch (SQLException e) { - throw new GigiApiException(e); - } + checkInsert(suffix); + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `domains` SET memid=?, domain=?")) { + ps.setInt(1, owner.getId()); + ps.setString(2, suffix); + ps.execute(); + id = ps.lastInsertId(); } + myCache.put(this); } public void delete() throws GigiApiException { if (id == 0) { throw new GigiApiException("not inserted."); } - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?"); - ps.setInt(1, id); - ps.execute(); - } catch (SQLException e) { - throw new GigiApiException(e); + synchronized (Domain.class) { + myCache.remove(this); + try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) { + ps.setInt(1, id); + ps.execute(); + } } } - public User getOwner() { + public CertificateOwner getOwner() { return owner; } + @Override public int getId() { return id; } @@ -96,75 +88,101 @@ public class Domain implements IdCachable { return suffix; } - public void addPing(String type, String config) throws GigiApiException { - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?"); + private LinkedList configs = null; + + public List getConfiguredPings() throws GigiApiException { + LinkedList configs = this.configs; + if (configs == null) { + configs = new LinkedList<>(); + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT id FROM pingconfig WHERE domainid=? AND `deleted` IS NULL")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + while (rs.next()) { + configs.add(DomainPingConfiguration.getById(rs.getInt(1))); + } + } + this.configs = configs; + + } + return Collections.unmodifiableList(configs); + } + + public void addPing(DomainPingType type, String config) throws GigiApiException { + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) { ps.setInt(1, id); - ps.setString(2, type); + ps.setString(2, type.toString().toLowerCase()); ps.setString(3, config); ps.execute(); - } catch (SQLException e) { - throw new GigiApiException(e); } + configs = null; } - public void verify(String hash) throws GigiApiException { - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)"); + public void clearPings() throws GigiApiException { + try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `pingconfig` SET `deleted`=CURRENT_TIMESTAMP WHERE `deleted` is NULL AND `domainid`=?")) { + ps.setInt(1, id); + ps.execute(); + } + configs = null; + } + + public synchronized void verify(String hash) throws GigiApiException { + try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domainPinglog` SET `state`='success' WHERE `challenge`=? AND `state`='open' AND `configId` IN (SELECT `id` FROM `pingconfig` WHERE `domainid`=? AND `type`='email')")) { ps.setString(1, hash); ps.setInt(2, id); ps.executeUpdate(); - } catch (SQLException e) { - throw new GigiApiException(e); } } public boolean isVerified() { - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'"); + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `domainid`=? AND `state`='success'")) { ps.setInt(1, id); - ResultSet rs = ps.executeQuery(); + GigiResultSet rs = ps.executeQuery(); return rs.next(); - } catch (SQLException e) { - e.printStackTrace(); } - return false; } - public String[][] getPings() throws GigiApiException { - try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;"); + public DomainPingExecution[] getPings() throws GigiApiException { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `state`, `type`, `info`, `result`, `configId`, `when` FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `pingconfig`.`domainid`=? ORDER BY `when` DESC;", true)) { ps.setInt(1, id); - ResultSet rs = ps.executeQuery(); + GigiResultSet rs = ps.executeQuery(); rs.last(); - String[][] contents = new String[rs.getRow()][]; + DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()]; rs.beforeFirst(); for (int i = 0; i < contents.length && rs.next(); i++) { - contents[i] = new String[] { - rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4) - }; + contents[i] = new DomainPingExecution(rs); } return contents; - } catch (SQLException e) { - throw new GigiApiException(e); } } - private static ObjectCache myCache = new ObjectCache<>(); + private static final ObjectCache myCache = new ObjectCache<>(); - public static Domain getById(int id) throws IllegalArgumentException { + public static synchronized Domain getById(int id) { Domain em = myCache.get(id); if (em == null) { - try { - synchronized (Domain.class) { - myCache.put(em = new Domain(id)); + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if ( !rs.next()) { + return null; } - } catch (SQLException e1) { - throw new IllegalArgumentException(e1); + myCache.put(em = new Domain(rs, id)); } } return em; } + public static Domain searchUserIdByDomain(String domain) { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ?")) { + ps.setString(1, domain); + GigiResultSet res = ps.executeQuery(); + if (res.next()) { + return getById(res.getInt(1)); + } else { + return null; + } + } + } + }