1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.io.InputStream;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.HashSet;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Properties;
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.database.GigiResultSet;
17 import org.cacert.gigi.util.PublicSuffixes;
19 public class Domain implements IdCachable, Verifyable {
21 private CertificateOwner owner;
23 private String suffix;
27 private static final Set<String> IDNEnabledTLDs;
30 Properties CPS = new Properties();
31 try (InputStream resourceAsStream = Domain.class.getResourceAsStream("CPS.properties")) {
32 CPS.load(resourceAsStream);
33 IDNEnabledTLDs = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(CPS.getProperty("IDN-enabled").split(","))));
34 } catch (IOException e) {
39 private Domain(int id) {
40 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL")) {
43 GigiResultSet rs = ps.executeQuery();
45 throw new IllegalArgumentException("Invalid domain id " + id);
48 owner = CertificateOwner.getById(rs.getInt(1));
49 suffix = rs.getString(2);
53 public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException {
54 synchronized (Domain.class) {
55 checkCertifyableDomain(suffix, actor.isInGroup(Group.CODESIGNING));
62 public static void checkCertifyableDomain(String s, boolean hasPunycodeRight) throws GigiApiException {
63 String[] parts = s.split("\\.", -1);
64 if (parts.length < 2) {
65 throw new GigiApiException("Domain does not contain '.'.");
67 for (int i = parts.length - 1; i >= 0; i--) {
68 if ( !isVaildDomainPart(parts[i], hasPunycodeRight)) {
69 throw new GigiApiException("Syntax error in Domain");
72 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(s);
73 if ( !s.equals(publicSuffix)) {
74 throw new GigiApiException("You may only register a domain with exactly one lable before the public suffix.");
76 if (("." + s).matches("(\\.[0-9]*)*")) {
77 // This is not reached because we currently have no TLD that is
78 // numbers only. But who knows..
79 // Better safe than sorry.
80 throw new GigiApiException("IP Addresses are not allowed");
82 checkPunycode(parts[0], s.substring(parts[0].length() + 1));
85 private static void checkPunycode(String label, String domainContext) throws GigiApiException {
86 if (label.charAt(2) != '-' || label.charAt(3) != '-') {
87 return; // is no punycode
89 if ( !IDNEnabledTLDs.contains(domainContext)) {
90 throw new GigiApiException("Punycode label could not be positively verified.");
92 if ( !label.startsWith("xn--")) {
93 throw new GigiApiException("Unknown ACE prefix.");
96 String unicode = IDN.toUnicode(label);
97 if (unicode.startsWith("xn--")) {
98 throw new GigiApiException("Punycode label could not be positively verified.");
100 } catch (IllegalArgumentException e) {
101 throw new GigiApiException("Punycode label could not be positively verified.");
105 public static boolean isVaildDomainPart(String s, boolean allowPunycode) {
106 if ( !s.matches("[a-z0-9-]+")) {
109 if (s.charAt(0) == '-' || s.charAt(s.length() - 1) == '-') {
112 if (s.length() > 63) {
115 boolean canBePunycode = s.length() >= 4 && s.charAt(2) == '-' && s.charAt(3) == '-';
116 if (canBePunycode && !allowPunycode) {
122 private static void checkInsert(String suffix) throws GigiApiException {
123 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")) {
124 ps.setString(1, suffix);
125 ps.setString(2, suffix);
126 ps.setString(3, suffix);
127 ps.setString(4, suffix);
128 GigiResultSet rs = ps.executeQuery();
129 boolean existed = rs.next();
132 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
137 private void insert() throws GigiApiException {
139 throw new GigiApiException("already inserted.");
142 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `domains` SET memid=?, domain=?")) {
143 ps.setInt(1, owner.getId());
144 ps.setString(2, suffix);
146 id = ps.lastInsertId();
151 public void delete() throws GigiApiException {
153 throw new GigiApiException("not inserted.");
155 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
161 public CertificateOwner getOwner() {
170 public String getSuffix() {
174 private LinkedList<DomainPingConfiguration> configs = null;
176 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
177 LinkedList<DomainPingConfiguration> configs = this.configs;
178 if (configs == null) {
179 configs = new LinkedList<>();
180 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT id FROM pingconfig WHERE domainid=? AND `deleted` IS NULL")) {
182 GigiResultSet rs = ps.executeQuery();
184 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
187 this.configs = configs;
190 return Collections.unmodifiableList(configs);
193 public void addPing(DomainPingType type, String config) throws GigiApiException {
194 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?")) {
196 ps.setString(2, type.toString().toLowerCase());
197 ps.setString(3, config);
203 public void clearPings() throws GigiApiException {
204 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `pingconfig` SET `deleted`=CURRENT_TIMESTAMP WHERE `deleted` is NULL AND `domainid`=?")) {
211 public synchronized void verify(String hash) throws GigiApiException {
212 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')")) {
213 ps.setString(1, hash);
219 public boolean isVerified() {
220 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `domainid`=? AND `state`='success'")) {
222 GigiResultSet rs = ps.executeQuery();
227 public DomainPingExecution[] getPings() throws GigiApiException {
228 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)) {
230 GigiResultSet rs = ps.executeQuery();
232 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
234 for (int i = 0; i < contents.length && rs.next(); i++) {
235 contents[i] = new DomainPingExecution(rs);
242 private static final ObjectCache<Domain> myCache = new ObjectCache<>();
244 public static synchronized Domain getById(int id) throws IllegalArgumentException {
245 Domain em = myCache.get(id);
247 myCache.put(em = new Domain(id));
252 public static Domain searchUserIdByDomain(String domain) {
253 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `domains` WHERE `domain` = ?")) {
254 ps.setString(1, domain);
255 GigiResultSet res = ps.executeQuery();
257 return getById(res.getInt(1));