1 package club.wpia.gigi.dbObjects;
3 import java.util.Collections;
4 import java.util.LinkedList;
7 import club.wpia.gigi.GigiApiException;
8 import club.wpia.gigi.database.GigiPreparedStatement;
9 import club.wpia.gigi.database.GigiResultSet;
10 import club.wpia.gigi.util.DomainAssessment;
12 public class Domain implements IdCachable, Verifyable {
14 private CertificateOwner owner;
16 private String suffix;
20 private Domain(GigiResultSet rs, int id) {
22 owner = CertificateOwner.getById(rs.getInt(1));
23 suffix = rs.getString(2);
26 public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException {
27 suffix = suffix.toLowerCase();
28 synchronized (Domain.class) {
29 DomainAssessment.checkCertifiableDomain(suffix, actor.isInGroup(Group.CODESIGNING), true);
36 private static void checkInsert(String suffix) throws GigiApiException {
37 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domains` WHERE (`domain`=? OR (CONCAT('.', `domain`)=RIGHT(?,LENGTH(`domain`)+1) OR RIGHT(`domain`,LENGTH(?)+1)=CONCAT('.',?::VARCHAR))) AND `deleted` IS NULL")) {
38 ps.setString(1, suffix);
39 ps.setString(2, suffix);
40 ps.setString(3, suffix);
41 ps.setString(4, suffix);
42 GigiResultSet rs = ps.executeQuery();
43 boolean existed = rs.next();
46 throw new GigiApiException("Domain could not be inserted. Domain is already known to the system.");
51 private void insert() throws GigiApiException {
53 throw new GigiApiException("already inserted.");
56 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `domains` SET memid=?, domain=?")) {
57 ps.setInt(1, owner.getId());
58 ps.setString(2, suffix);
60 id = ps.lastInsertId();
65 public void delete() throws GigiApiException {
67 throw new GigiApiException("not inserted.");
69 synchronized (Domain.class) {
71 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
78 public CertificateOwner getOwner() {
87 public String getSuffix() {
91 private LinkedList<DomainPingConfiguration> configs = null;
93 public List<DomainPingConfiguration> getConfiguredPings() {
94 LinkedList<DomainPingConfiguration> configs = this.configs;
95 if (configs == null) {
96 configs = new LinkedList<>();
97 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT id FROM pingconfig WHERE domainid=? AND `deleted` IS NULL")) {
99 GigiResultSet rs = ps.executeQuery();
101 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
104 this.configs = configs;
107 return Collections.unmodifiableList(configs);
110 public void addPing(DomainPingType type, String config) throws GigiApiException {
111 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) {
114 ps.setString(3, config);
120 public void clearPings() throws GigiApiException {
121 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `pingconfig` SET `deleted`=CURRENT_TIMESTAMP WHERE `deleted` is NULL AND `domainid`=?")) {
128 public synchronized boolean isVerifyable(String hash) throws GigiApiException {
129 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domainPinglog` WHERE `challenge`=? AND `state`='open' AND `configId` IN (SELECT `id` FROM `pingconfig` WHERE `domainid`=? AND `type`='email')")) {
130 ps.setString(1, hash);
132 return ps.executeQuery().next();
136 public synchronized void verify(String hash) throws GigiApiException {
137 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')")) {
138 ps.setString(1, hash);
140 if ( !ps.executeMaybeUpdate()) {
141 throw new IllegalArgumentException("Given token could not be found to complete the verification process (Domain Ping).");
147 * Determines current domain validity. A domain is valid, iff at least two
148 * configured pings are currently successful.
150 * @return true, iff domain is valid
151 * @throws GigiApiException
153 public boolean isVerified() {
155 boolean[] used = new boolean[DomainPingType.values().length];
156 for (DomainPingConfiguration config : getConfiguredPings()) {
157 if (config.isValid() && !used[config.getType().ordinal()]) {
159 used[config.getType().ordinal()] = true;
168 public DomainPingExecution[] getPings() throws GigiApiException {
169 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)) {
171 GigiResultSet rs = ps.executeQuery();
173 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
175 for (int i = 0; i < contents.length && rs.next(); i++) {
176 contents[i] = new DomainPingExecution(rs);
183 private static final ObjectCache<Domain> myCache = new ObjectCache<>();
185 public static synchronized Domain getById(int id) {
186 Domain em = myCache.get(id);
188 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
190 GigiResultSet rs = ps.executeQuery();
194 myCache.put(em = new Domain(rs, id));
200 public static Domain searchDomain(String domain) {
201 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ? AND `deleted` IS NULL")) {
202 ps.setString(1, domain);
203 GigiResultSet res = ps.executeQuery();
205 return getById(res.getInt(1));
212 public Certificate[] fetchActiveCertificates() {
213 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `certs`.`id` FROM `certs` INNER JOIN `subjectAlternativeNames` ON `subjectAlternativeNames`.`certId` = `certs`.`id` WHERE (`contents`=? OR RIGHT(`contents`,LENGTH(?)+1)=CONCAT('.',?::VARCHAR)) AND `type`='DNS' AND `revoked` IS NULL AND `expire` > CURRENT_TIMESTAMP AND `memid`=? GROUP BY `certs`.`id`", true)) {
214 ps.setString(1, suffix);
215 ps.setString(2, suffix);
216 ps.setString(3, suffix);
217 ps.setInt(4, owner.getId());
218 GigiResultSet rs = ps.executeQuery();
220 Certificate[] res = new Certificate[rs.getRow()];
224 res[i++] = Certificate.getById(rs.getInt(1));