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.DatabaseConnection;
16 import org.cacert.gigi.database.GigiPreparedStatement;
17 import org.cacert.gigi.database.GigiResultSet;
18 import org.cacert.gigi.util.PublicSuffixes;
20 public class Domain implements IdCachable, Verifyable {
22 private CertificateOwner owner;
24 private String suffix;
28 private static final Set<String> IDNEnabledTLDs;
31 Properties CPS = new Properties();
32 try (InputStream resourceAsStream = Domain.class.getResourceAsStream("CPS.properties")) {
33 CPS.load(resourceAsStream);
34 IDNEnabledTLDs = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(CPS.getProperty("IDN-enabled").split(","))));
35 } catch (IOException e) {
40 private Domain(int id) {
41 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid`, `domain` FROM `domains` WHERE `id`=? AND `deleted` IS NULL");
44 GigiResultSet rs = ps.executeQuery();
46 throw new IllegalArgumentException("Invalid domain id " + id);
49 owner = CertificateOwner.getById(rs.getInt(1));
50 suffix = rs.getString(2);
54 public Domain(User actor, CertificateOwner owner, String suffix) throws GigiApiException {
55 synchronized (Domain.class) {
56 checkCertifyableDomain(suffix, actor.isInGroup(Group.CODESIGNING));
63 public static void checkCertifyableDomain(String s, boolean hasPunycodeRight) throws GigiApiException {
64 String[] parts = s.split("\\.", -1);
65 if (parts.length < 2) {
66 throw new GigiApiException("Domain does not contain '.'.");
68 for (int i = parts.length - 1; i >= 0; i--) {
69 if ( !isVaildDomainPart(parts[i], hasPunycodeRight)) {
70 throw new GigiApiException("Syntax error in Domain");
73 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(s);
74 if ( !s.equals(publicSuffix)) {
75 throw new GigiApiException("You may only register a domain with exactly one lable before the public suffix.");
77 checkPunycode(parts[0], s.substring(parts[0].length() + 1));
80 private static void checkPunycode(String label, String domainContext) throws GigiApiException {
81 if (label.charAt(2) != '-' || label.charAt(3) != '-') {
82 return; // is no punycode
84 if ( !IDNEnabledTLDs.contains(domainContext)) {
85 throw new GigiApiException("Punycode label could not be positively verified.");
87 if ( !label.startsWith("xn--")) {
88 throw new GigiApiException("Unknown ACE prefix.");
91 String unicode = IDN.toUnicode(label);
92 if (unicode.startsWith("xn--")) {
93 throw new GigiApiException("Punycode label could not be positively verified.");
95 } catch (IllegalArgumentException e) {
96 throw new GigiApiException("Punycode label could not be positively verified.");
100 public static boolean isVaildDomainPart(String s, boolean allowPunycode) {
101 if ( !s.matches("[a-z0-9-]+")) {
104 if (s.charAt(0) == '-' || s.charAt(s.length() - 1) == '-') {
107 if (s.length() > 63) {
110 boolean canBePunycode = s.length() >= 4 && s.charAt(2) == '-' && s.charAt(3) == '-';
111 if (canBePunycode && !allowPunycode) {
117 private static void checkInsert(String suffix) throws GigiApiException {
118 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");
119 ps.setString(1, suffix);
120 ps.setString(2, suffix);
121 ps.setString(3, suffix);
122 ps.setString(4, suffix);
123 GigiResultSet rs = ps.executeQuery();
124 boolean existed = rs.next();
127 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
131 private void insert() throws GigiApiException {
133 throw new GigiApiException("already inserted.");
136 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
137 ps.setInt(1, owner.getId());
138 ps.setString(2, suffix);
140 id = ps.lastInsertId();
144 public void delete() throws GigiApiException {
146 throw new GigiApiException("not inserted.");
148 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?");
153 public CertificateOwner getOwner() {
162 public String getSuffix() {
166 private LinkedList<DomainPingConfiguration> configs = null;
168 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
169 LinkedList<DomainPingConfiguration> configs = this.configs;
170 if (configs == null) {
171 configs = new LinkedList<>();
172 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
174 GigiResultSet rs = ps.executeQuery();
176 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
179 this.configs = configs;
182 return Collections.unmodifiableList(configs);
185 public void addPing(DomainPingType type, String config) throws GigiApiException {
186 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `pingconfig` SET `domainid`=?, `type`=?::`pingType`, `info`=?");
188 ps.setString(2, type.toString().toLowerCase());
189 ps.setString(3, config);
194 public synchronized void verify(String hash) throws GigiApiException {
195 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domainPinglog` SET `state`='success' WHERE `challenge`=? AND `state`='open' AND `configId` IN (SELECT `id` FROM `pingconfig` WHERE `domainid`=? AND `type`='email')");
196 ps.setString(1, hash);
201 public boolean isVerified() {
202 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `domainid`=? AND `state`='success'");
204 GigiResultSet rs = ps.executeQuery();
208 public DomainPingExecution[] getPings() throws GigiApiException {
209 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepareScrollable("SELECT `state`, `type`, `info`, `result`, `configId`, `when` FROM `domainPinglog` INNER JOIN `pingconfig` ON `pingconfig`.`id`=`domainPinglog`.`configId` WHERE `pingconfig`.`domainid`=? ORDER BY `when` DESC;");
211 GigiResultSet rs = ps.executeQuery();
213 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
215 for (int i = 0; i < contents.length && rs.next(); i++) {
216 contents[i] = new DomainPingExecution(rs);
222 private static final ObjectCache<Domain> myCache = new ObjectCache<>();
224 public static synchronized Domain getById(int id) throws IllegalArgumentException {
225 Domain em = myCache.get(id);
227 myCache.put(em = new Domain(id));
232 public static int searchUserIdByDomain(String domain) {
233 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid` FROM `domains` WHERE `domain` = ?");
234 ps.setString(1, domain);
235 GigiResultSet res = ps.executeQuery();
237 return res.getInt(1);