]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Domain.java
upd: cleanup SQL statements to make them statically verifiable.
[gigi.git] / src / org / cacert / gigi / dbObjects / Domain.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.Collections;
4 import java.util.LinkedList;
5 import java.util.List;
6
7 import org.cacert.gigi.GigiApiException;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
10 import org.cacert.gigi.util.DomainAssessment;
11
12 public class Domain implements IdCachable, Verifyable {
13
14     private CertificateOwner owner;
15
16     private String suffix;
17
18     private int id;
19
20     private Domain(GigiResultSet rs, int id) {
21         this.id = id;
22         owner = CertificateOwner.getById(rs.getInt(1));
23         suffix = rs.getString(2);
24     }
25
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);
30             this.owner = owner;
31             this.suffix = suffix;
32             insert();
33         }
34     }
35
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('.',?))) 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();
44             rs.close();
45             if (existed) {
46                 throw new GigiApiException("Domain could not be inserted. Domain is already known to the system.");
47             }
48         }
49     }
50
51     private void insert() throws GigiApiException {
52         if (id != 0) {
53             throw new GigiApiException("already inserted.");
54         }
55         checkInsert(suffix);
56         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `domains` SET memid=?, domain=?")) {
57             ps.setInt(1, owner.getId());
58             ps.setString(2, suffix);
59             ps.execute();
60             id = ps.lastInsertId();
61         }
62         myCache.put(this);
63     }
64
65     public void delete() throws GigiApiException {
66         if (id == 0) {
67             throw new GigiApiException("not inserted.");
68         }
69         synchronized (Domain.class) {
70             myCache.remove(this);
71             try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
72                 ps.setInt(1, id);
73                 ps.execute();
74             }
75         }
76     }
77
78     public CertificateOwner getOwner() {
79         return owner;
80     }
81
82     @Override
83     public int getId() {
84         return id;
85     }
86
87     public String getSuffix() {
88         return suffix;
89     }
90
91     private LinkedList<DomainPingConfiguration> configs = null;
92
93     public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
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")) {
98                 ps.setInt(1, id);
99                 GigiResultSet rs = ps.executeQuery();
100                 while (rs.next()) {
101                     configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
102                 }
103             }
104             this.configs = configs;
105
106         }
107         return Collections.unmodifiableList(configs);
108     }
109
110     public void addPing(DomainPingType type, String config) throws GigiApiException {
111         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) {
112             ps.setInt(1, id);
113             ps.setEnum(2, type);
114             ps.setString(3, config);
115             ps.execute();
116         }
117         configs = null;
118     }
119
120     public void clearPings() throws GigiApiException {
121         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `pingconfig` SET `deleted`=CURRENT_TIMESTAMP WHERE `deleted` is NULL AND `domainid`=?")) {
122             ps.setInt(1, id);
123             ps.execute();
124         }
125         configs = null;
126     }
127
128     public synchronized void verify(String hash) throws GigiApiException {
129         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')")) {
130             ps.setString(1, hash);
131             ps.setInt(2, id);
132             ps.executeUpdate();
133         }
134     }
135
136     public boolean isVerified() {
137         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `domainid`=? AND `state`='success'")) {
138             ps.setInt(1, id);
139             GigiResultSet rs = ps.executeQuery();
140             return rs.next();
141         }
142     }
143
144     public DomainPingExecution[] getPings() throws GigiApiException {
145         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)) {
146             ps.setInt(1, id);
147             GigiResultSet rs = ps.executeQuery();
148             rs.last();
149             DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
150             rs.beforeFirst();
151             for (int i = 0; i < contents.length && rs.next(); i++) {
152                 contents[i] = new DomainPingExecution(rs);
153             }
154             return contents;
155         }
156
157     }
158
159     private static final ObjectCache<Domain> myCache = new ObjectCache<>();
160
161     public static synchronized Domain getById(int id) {
162         Domain em = myCache.get(id);
163         if (em == null) {
164             try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
165                 ps.setInt(1, id);
166                 GigiResultSet rs = ps.executeQuery();
167                 if ( !rs.next()) {
168                     return null;
169                 }
170                 myCache.put(em = new Domain(rs, id));
171             }
172         }
173         return em;
174     }
175
176     public static Domain searchDomain(String domain) {
177         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ? AND `deleted` IS NULL")) {
178             ps.setString(1, domain);
179             GigiResultSet res = ps.executeQuery();
180             if (res.next()) {
181                 return getById(res.getInt(1));
182             } else {
183                 return null;
184             }
185         }
186     }
187
188 }