1 package org.cacert.gigi.email;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.OutputStreamWriter;
7 import java.io.PrintWriter;
8 import java.net.Socket;
9 import java.security.GeneralSecurityException;
10 import java.security.Key;
11 import java.security.PrivateKey;
12 import java.security.cert.Certificate;
13 import java.security.cert.X509Certificate;
14 import java.util.Arrays;
15 import java.util.Comparator;
16 import java.util.Properties;
17 import java.util.regex.Pattern;
19 import javax.naming.NamingException;
20 import javax.net.ssl.SSLSocketFactory;
22 import org.cacert.gigi.crypto.SMIME;
23 import org.cacert.gigi.database.GigiPreparedStatement;
24 import org.cacert.gigi.util.DNSUtil;
25 import org.cacert.gigi.util.DomainAssessment;
27 public abstract class EmailProvider {
29 public abstract void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
31 private static EmailProvider instance;
33 private X509Certificate c;
37 protected void init(Certificate c, Key k) {
38 this.c = (X509Certificate) c;
39 this.k = (PrivateKey) k;
42 protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
43 if (k == null || c == null) {
44 output.print(contents);
46 SMIME.smime(contents, k, c, output);
50 public static EmailProvider getInstance() {
54 protected static void setInstance(EmailProvider instance) {
55 EmailProvider.instance = instance;
58 public static void initSystem(Properties conf, Certificate cert, Key pk) {
60 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
61 EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
64 } catch (ReflectiveOperationException e) {
69 public static final String OK = "OK";
71 public static final String FAIL = "FAIL";
73 private static final String MAIL_P_RFC_WORD = "[A-Za-z0-9\\+\\.!#$%&'*/=?^_`|~{}-]+";
75 private static final String MAIL_P_RFC_LOCAL = MAIL_P_RFC_WORD + "(?:\\." + MAIL_P_RFC_WORD + ")*";
77 private static final String MAIL_P_RFC_LABEL = "(?!(?!xn)..--|-)(?:[A-Za-z0-9-]+)(?<!-)";
79 private static final String MAIL_P_RFC_ADDRESS = MAIL_P_RFC_LOCAL + "@(?:" + MAIL_P_RFC_LABEL + "\\.)+" + MAIL_P_RFC_LABEL + "\\.?";
81 private static final Pattern MAIL_LOCAL = Pattern.compile("^" + MAIL_P_RFC_LOCAL + "$");
83 private static final Pattern MAIL_ADDRESS = Pattern.compile("^" + MAIL_P_RFC_ADDRESS + "$");
85 public String checkEmailServer(int forUid, String address) throws IOException {
86 if (isValidMailAddress(address)) {
87 String[] parts = address.split("@", 2);
88 String domain = parts[1];
92 mxhosts = DNSUtil.getMXEntries(domain);
93 } catch (NamingException e1) {
94 return "MX lookup for your hostname failed.";
98 for (String host : mxhosts) {
99 host = host.split(" ", 2)[1];
100 if (host.endsWith(".")) {
101 host = host.substring(0, host.length() - 1);
103 return "Strange MX records.";
105 try (Socket s = new Socket(host, 25);
106 BufferedReader br0 = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));//
107 PrintWriter pw0 = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"))) {
108 BufferedReader br = br0;
109 PrintWriter pw = pw0;
111 if ( !SendMail.readSMTPResponse(br, 220)) {
115 pw.print("EHLO www.cacert.org\r\n");
117 boolean starttls = false;
119 line = br.readLine();
123 starttls |= line.substring(4).equals("STARTTLS");
124 } while (line.startsWith("250-"));
125 if (line == null || !line.startsWith("250 ")) {
130 pw.print("STARTTLS\r\n");
132 if ( !SendMail.readSMTPResponse(br, 220)) {
135 Socket s1 = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(s, host, 25, true);
136 br = new BufferedReader(new InputStreamReader(s1.getInputStream(), "UTF-8"));
137 pw = new PrintWriter(new OutputStreamWriter(s1.getOutputStream(), "UTF-8"));
138 pw.print("EHLO www.cacert.org\r\n");
140 if ( !SendMail.readSMTPResponse(br, 250)) {
145 pw.print("MAIL FROM: <returns@cacert.org>\r\n");
148 if ( !SendMail.readSMTPResponse(br, 250)) {
151 pw.print("RCPT TO: <" + address + ">\r\n");
154 if ( !SendMail.readSMTPResponse(br, 250)) {
157 pw.print("QUIT\r\n");
159 if ( !SendMail.readSMTPResponse(br, 221)) {
163 try (GigiPreparedStatement statmt = new GigiPreparedStatement("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`='success'::`pingState`")) {
164 statmt.setString(1, address);
165 statmt.setString(2, line);
166 statmt.setInt(3, forUid);
170 if (line == null || !line.startsWith("250")) {
179 try (GigiPreparedStatement statmt = new GigiPreparedStatement("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast'::`emailPingType`, `status`='failed'::`pingState`")) {
180 statmt.setString(1, address);
181 statmt.setString(2, "Failed to make a connection to the mail server");
182 statmt.setInt(3, forUid);
188 private static void sortMX(String[] mxhosts) {
189 Arrays.sort(mxhosts, new Comparator<String>() {
192 public int compare(String o1, String o2) {
193 int i1 = Integer.parseInt(o1.split(" ")[0]);
194 int i2 = Integer.parseInt(o2.split(" ")[0]);
195 return Integer.compare(i1, i2);
200 public static boolean isValidMailAddress(String address) {
201 if ( !MAIL_ADDRESS.matcher(address).matches()) {
205 String[] parts = address.split("@", 2);
207 String local = parts[0];
208 String domain = parts[1];
210 if ( !MAIL_LOCAL.matcher(local).matches()) {
214 for (String domainPart : domain.split("\\.", -1)) {
215 if ( !DomainAssessment.isValidDomainPart(domainPart)) {