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