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.dbObjects.DomainPingConfiguration.PingType;
19 import org.cacert.gigi.util.PublicSuffixes;
21 public class Domain implements IdCachable {
23 public class DomainPingExecution {
31 private String result;
33 private DomainPingConfiguration config;
35 public DomainPingExecution(GigiResultSet rs) {
36 state = rs.getString(1);
37 type = rs.getString(2);
38 info = rs.getString(3);
39 result = rs.getString(4);
40 config = DomainPingConfiguration.getById(rs.getInt(5));
43 public String getState() {
47 public String getType() {
51 public String getInfo() {
55 public String getResult() {
59 public DomainPingConfiguration getConfig() {
67 private String suffix;
71 private static final Set<String> IDNEnabledTLDs;
73 Properties CPS = new Properties();
74 try (InputStream resourceAsStream = Domain.class.getResourceAsStream("CPS.properties")) {
75 CPS.load(resourceAsStream);
76 IDNEnabledTLDs = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(CPS.getProperty("IDN-enabled").split(","))));
77 } catch (IOException e) {
82 private Domain(int id) {
83 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, domain FROM `domains` WHERE id=? AND deleted IS NULL");
86 GigiResultSet rs = ps.executeQuery();
88 throw new IllegalArgumentException("Invalid domain id " + id);
91 owner = User.getById(rs.getInt(1));
92 suffix = rs.getString(2);
96 public Domain(User owner, String suffix) throws GigiApiException {
97 checkCertifyableDomain(suffix, owner.isInGroup(Group.CODESIGNING));
103 public static void checkCertifyableDomain(String s, boolean hasPunycodeRight) throws GigiApiException {
104 String[] parts = s.split("\\.", -1);
105 if (parts.length < 2) {
106 throw new GigiApiException("Domain does not contain '.'.");
108 for (int i = parts.length - 1; i >= 0; i--) {
109 if ( !isVaildDomainPart(parts[i], hasPunycodeRight)) {
110 throw new GigiApiException("Syntax error in Domain");
113 String publicSuffix = PublicSuffixes.getInstance().getRegistrablePart(s);
114 if ( !s.equals(publicSuffix)) {
115 throw new GigiApiException("You may only register a domain with exactly one lable before the public suffix.");
117 checkPunycode(parts[0], s.substring(parts[0].length() + 1));
120 private static void checkPunycode(String label, String domainContext) throws GigiApiException {
121 if (label.charAt(2) != '-' || label.charAt(3) != '-') {
122 return; // is no punycode
124 if ( !IDNEnabledTLDs.contains(domainContext)) {
125 throw new GigiApiException("Punycode label could not be positively verified.");
127 if ( !label.startsWith("xn--")) {
128 throw new GigiApiException("Unknown ACE prefix.");
131 String unicode = IDN.toUnicode(label);
132 if (unicode.startsWith("xn--")) {
133 throw new GigiApiException("Punycode label could not be positively verified.");
135 } catch (IllegalArgumentException e) {
136 throw new GigiApiException("Punycode label could not be positively verified.");
140 public static boolean isVaildDomainPart(String s, boolean allowPunycode) {
141 if ( !s.matches("[a-z0-9-]+")) {
144 if (s.charAt(0) == '-' || s.charAt(s.length() - 1) == '-') {
147 if (s.length() > 63) {
150 boolean canBePunycode = s.length() >= 4 && s.charAt(2) == '-' && s.charAt(3) == '-';
151 if (canBePunycode && !allowPunycode) {
157 private static void checkInsert(String suffix) throws GigiApiException {
158 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");
159 ps.setString(1, suffix);
160 ps.setString(2, suffix);
161 ps.setString(3, suffix);
162 ps.setString(4, suffix);
163 GigiResultSet rs = ps.executeQuery();
164 boolean existed = rs.next();
167 throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
171 public void insert() throws GigiApiException {
172 synchronized (Domain.class) {
174 throw new GigiApiException("already inserted.");
177 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `domains` SET memid=?, domain=?");
178 ps.setInt(1, owner.getId());
179 ps.setString(2, suffix);
181 id = ps.lastInsertId();
186 public void delete() throws GigiApiException {
188 throw new GigiApiException("not inserted.");
190 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `domains` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
195 public User getOwner() {
204 public String getSuffix() {
208 private LinkedList<DomainPingConfiguration> configs = null;
210 public List<DomainPingConfiguration> getConfiguredPings() throws GigiApiException {
211 LinkedList<DomainPingConfiguration> configs = this.configs;
212 if (configs == null) {
213 configs = new LinkedList<>();
214 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM pingconfig WHERE domainid=?");
216 GigiResultSet rs = ps.executeQuery();
218 configs.add(DomainPingConfiguration.getById(rs.getInt(1)));
221 this.configs = configs;
224 return Collections.unmodifiableList(configs);
227 public void addPing(PingType type, String config) throws GigiApiException {
228 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
230 ps.setString(2, type.toString().toLowerCase());
231 ps.setString(3, config);
236 public void verify(String hash) throws GigiApiException {
237 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE domainPinglog SET state='success' WHERE challenge=? AND configId IN (SELECT id FROM pingconfig WHERE domainId=?)");
238 ps.setString(1, hash);
243 public boolean isVerified() {
244 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM domainPinglog INNER JOIN pingconfig ON pingconfig.id=domainPinglog.configId WHERE domainid=? AND state='success'");
246 GigiResultSet rs = ps.executeQuery();
250 public DomainPingExecution[] getPings() throws GigiApiException {
251 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;");
253 GigiResultSet rs = ps.executeQuery();
255 DomainPingExecution[] contents = new DomainPingExecution[rs.getRow()];
257 for (int i = 0; i < contents.length && rs.next(); i++) {
258 contents[i] = new DomainPingExecution(rs);
264 private static ObjectCache<Domain> myCache = new ObjectCache<>();
266 public static synchronized Domain getById(int id) throws IllegalArgumentException {
267 Domain em = myCache.get(id);
269 myCache.put(em = new Domain(id));
274 public static int searchUserIdByDomain(String domain) {
275 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid FROM domains WHERE domain = ?");
276 ps.setString(1, domain);
277 GigiResultSet res = ps.executeQuery();
280 return res.getInt(1);