]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Domain.java
043277cf4ab3f2108a90efe040635e25d172ec12
[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         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
70             ps.setInt(1, id);
71             ps.execute();
72         }
73     }
74
75     public CertificateOwner getOwner() {
76         return owner;
77     }
78
79     @Override
80     public int getId() {
81         return id;
82     }
83
84     public String getSuffix() {
85         return suffix;
86     }
87
88     private LinkedList<DomainPingConfiguration> configs = null;
89
90     public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
91         LinkedList<DomainPingConfiguration> configs = this.configs;
92         if (configs == null) {
93             configs = new LinkedList<>();
94             try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT id FROM pingconfig WHERE domainid=? AND `deleted` IS NULL")) {
95                 ps.setInt(1, id);
96                 GigiResultSet rs = ps.executeQuery();
97                 while (rs.next()) {
98                     configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
99                 }
100             }
101             this.configs = configs;
102
103         }
104         return Collections.unmodifiableList(configs);
105     }
106
107     public void addPing(DomainPingType type, String config) throws GigiApiException {
108         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) {
109             ps.setInt(1, id);
110             ps.setString(2, type.toString().toLowerCase());
111             ps.setString(3, config);
112             ps.execute();
113         }
114         configs = null;
115     }
116
117     public void clearPings() throws GigiApiException {
118         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `pingconfig` SET `deleted`=CURRENT_TIMESTAMP WHERE `deleted` is NULL AND `domainid`=?")) {
119             ps.setInt(1, id);
120             ps.execute();
121         }
122         configs = null;
123     }
124
125     public synchronized void verify(String hash) throws GigiApiException {
126         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')")) {
127             ps.setString(1, hash);
128             ps.setInt(2, id);
129             ps.executeUpdate();
130         }
131     }
132
133     public boolean isVerified() {
134         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `domainid`=? AND `state`='success'")) {
135             ps.setInt(1, id);
136             GigiResultSet rs = ps.executeQuery();
137             return rs.next();
138         }
139     }
140
141     public DomainPingExecution[] getPings() throws GigiApiException {
142         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)) {
143             ps.setInt(1, id);
144             GigiResultSet rs = ps.executeQuery();
145             rs.last();
146             DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
147             rs.beforeFirst();
148             for (int i = 0; i < contents.length && rs.next(); i++) {
149                 contents[i] = new DomainPingExecution(rs);
150             }
151             return contents;
152         }
153
154     }
155
156     private static final ObjectCache<Domain> myCache = new ObjectCache<>();
157
158     public static synchronized Domain getById(int id) {
159         Domain em = myCache.get(id);
160         if (em == null) {
161             try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
162                 ps.setInt(1, id);
163                 GigiResultSet rs = ps.executeQuery();
164                 if ( !rs.next()) {
165                     return null;
166                 }
167                 myCache.put(em = new Domain(rs, id));
168             }
169         }
170         return em;
171     }
172
173     public static Domain searchUserIdByDomain(String domain) {
174         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ?")) {
175             ps.setString(1, domain);
176             GigiResultSet res = ps.executeQuery();
177             if (res.next()) {
178                 return getById(res.getInt(1));
179             } else {
180                 return null;
181             }
182         }
183     }
184
185 }