1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.LinkedList;
10 import java.util.Properties;
13 import org.cacert.gigi.GigiApiException;
14 import org.cacert.gigi.database.DatabaseConnection;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.database.GigiResultSet;
17 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
18 import org.cacert.gigi.util.PublicSuffixes;
20 public class Domain implements IdCachable {
22 public class DomainPingExecution {
30 private String result;
32 private DomainPingConfiguration config;
34 public DomainPingExecution(GigiResultSet rs) {
35 state = rs.getString(1);
36 type = rs.getString(2);
37 info = rs.getString(3);
38 result = rs.getString(4);
39 config = DomainPingConfiguration.getById(rs.getInt(5));
42 public String getState() {
46 public String getType() {
50 public String getInfo() {
54 public String getResult() {
58 public DomainPingConfiguration getConfig() {
66 private String suffix;
70 private static final Set<String> IDNEnabledTLDs;
72 Properties CPS = new Properties();
74 CPS.load(Domain.class.getResourceAsStream("CPS.properties"));
75 IDNEnabledTLDs = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(CPS.getProperty("IDN-enabled").split(","))));
76 } catch (IOException e) {
81 private Domain(int id) {
82 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
85 GigiResultSet rs = ps.executeQuery();
87 throw new IllegalArgumentException("Invalid domain id " + id);
90 owner = User.getById(rs.getInt(1));
91 suffix = rs.getString(2);
95 public Domain(User owner, String suffix) throws GigiApiException {
96 checkCertifyableDomain(suffix, owner.isInGroup(Group.getByString("codesign")));
102 public static void checkCertifyableDomain(String s, boolean hasPunycodeRight) throws GigiApiException {
103 String[] parts = s.split("\\.", -1);
104 if (parts.length < 2) {
105 throw new GigiApiException("Domain does not contain '.'.");
107 for (int i = parts.length - 1; i >= 0; i--) {
108 if ( !isVaildDomainPart(parts[i], hasPunycodeRight)) {
109 throw new GigiApiException("Syntax error in Domain");
112 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(s);
113 if ( !s.equals(publicSuffix)) {
114 throw new GigiApiException("You may only register a domain with exactly one lable before the public suffix.");
116 checkPunycode(parts[0], s.substring(parts[0].length() + 1));
119 private static void checkPunycode(String label, String domainContext) throws GigiApiException {
120 if (label.charAt(2) != '-' || label.charAt(3) != '-') {
121 return; // is no punycode
123 if ( !IDNEnabledTLDs.contains(domainContext)) {
124 throw new GigiApiException("Punycode label could not be positively verified.");
126 if ( !label.startsWith("xn--")) {
127 throw new GigiApiException("Unknown ACE prefix.");
130 String unicode = IDN.toUnicode(label);
131 if (unicode.startsWith("xn--")) {
132 throw new GigiApiException("Punycode label could not be positively verified.");
134 } catch (IllegalArgumentException e) {
135 throw new GigiApiException("Punycode label could not be positively verified.");
139 public static boolean isVaildDomainPart(String s, boolean allowPunycode) {
140 if ( !s.matches("[a-z0-9-]+")) {
143 if (s.charAt(0) == '-' || s.charAt(s.length() - 1) == '-') {
146 if (s.length() > 63) {
149 boolean canBePunycode = s.length() >= 4 && s.charAt(2) == '-' && s.charAt(3) == '-';
150 if (canBePunycode && !allowPunycode) {
156 private static void checkInsert(String suffix) throws GigiApiException {
157 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domains` WHERE (domain=? OR (CONCAT('.', domain)=RIGHT(?,LENGTH(domain)+1) OR RIGHT(domain,LENGTH(?)+1)=CONCAT('.',?))) AND deleted IS NULL");
158 ps.setString(1, suffix);
159 ps.setString(2, suffix);
160 ps.setString(3, suffix);
161 ps.setString(4, suffix);
162 GigiResultSet rs = ps.executeQuery();
163 boolean existed = rs.next();
166 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
170 public void insert() throws GigiApiException {
172 throw new GigiApiException("already inserted.");
174 synchronized (Domain.class) {
176 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
177 ps.setInt(1, owner.getId());
178 ps.setString(2, suffix);
180 id = ps.lastInsertId();
185 public void delete() throws GigiApiException {
187 throw new GigiApiException("not inserted.");
189 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
194 public User getOwner() {
203 public String getSuffix() {
207 private LinkedList<DomainPingConfiguration> configs = null;
209 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
210 LinkedList<DomainPingConfiguration> configs = this.configs;
211 if (configs == null) {
212 configs = new LinkedList<>();
213 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
215 GigiResultSet rs = ps.executeQuery();
217 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
220 this.configs = configs;
223 return Collections.unmodifiableList(configs);
226 public void addPing(PingType type, String config) throws GigiApiException {
227 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
229 ps.setString(2, type.toString().toLowerCase());
230 ps.setString(3, config);
235 public void verify(String hash) throws GigiApiException {
236 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
237 ps.setString(1, hash);
242 public boolean isVerified() {
243 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
245 GigiResultSet rs = ps.executeQuery();
249 public DomainPingExecution[] getPings() throws GigiApiException {
250 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT state, type, info, result, configId FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configid WHERE pingconfig.domainid=? ORDER BY `when` DESC;");
252 GigiResultSet rs = ps.executeQuery();
254 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
256 for (int i = 0; i < contents.length && rs.next(); i++) {
257 contents[i] = new DomainPingExecution(rs);
263 private static ObjectCache<Domain> myCache = new ObjectCache<>();
265 public static synchronized Domain getById(int id) throws IllegalArgumentException {
266 Domain em = myCache.get(id);
268 myCache.put(em = new Domain(id));
273 public static int searchUserIdByDomain(String domain) {
274 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid FROM domains WHERE domain = ?");
275 ps.setString(1, domain);
276 GigiResultSet res = ps.executeQuery();
279 return res.getInt(1);