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.PrintWriter;
7 import java.net.Socket;
8 import java.security.GeneralSecurityException;
9 import java.security.Key;
10 import java.security.PrivateKey;
11 import java.security.cert.Certificate;
12 import java.security.cert.X509Certificate;
13 import java.sql.PreparedStatement;
14 import java.sql.SQLException;
15 import java.util.Properties;
16 import java.util.regex.Pattern;
18 import javax.naming.NamingException;
20 import org.cacert.gigi.crypto.SMIME;
21 import org.cacert.gigi.database.DatabaseConnection;
22 import org.cacert.gigi.util.DNSUtil;
24 public abstract class EmailProvider {
26 public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
28 private static EmailProvider instance;
30 private X509Certificate c;
34 protected final void init(Certificate c, Key k) {
35 this.c = (X509Certificate) c;
36 this.k = (PrivateKey) k;
39 protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
40 SMIME.smime(contents, k, c, output);
43 public static EmailProvider getInstance() {
47 protected static void setInstance(EmailProvider instance) {
48 EmailProvider.instance = instance;
51 public static void initSystem(Properties conf, Certificate cert, Key pk) {
53 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
54 EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
57 } catch (ReflectiveOperationException e) {
62 public static final String OK = "OK";
64 public static final String FAIL = "FAIL";
66 public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
68 public String checkEmailServer(int forUid, String address) throws IOException {
69 if (MAIL.matcher(address).matches()) {
70 String[] parts = address.split("@", 2);
71 String domain = parts[1];
75 mxhosts = DNSUtil.getMXEntries(domain);
76 } catch (NamingException e1) {
77 return "MX lookup for your hostname failed.";
80 for (String host : mxhosts) {
81 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
83 while ((line = br.readLine()) != null && line.startsWith("220-")) {
85 if (line == null || !line.startsWith("220")) {
89 pw.print("HELO www.cacert.org\r\n");
92 while ((line = br.readLine()) != null && line.startsWith("220")) {
95 if (line == null || !line.startsWith("250")) {
98 pw.print("MAIL FROM: <returns@cacert.org>\r\n");
101 line = br.readLine();
103 if (line == null || !line.startsWith("250")) {
106 pw.print("RCPT TO: <" + address + ">\r\n");
109 line = br.readLine();
110 pw.print("QUIT\r\n");
114 PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
115 statmt.setString(1, address);
116 statmt.setString(2, line);
117 statmt.setInt(3, forUid);
119 } catch (SQLException e) {
123 if (line == null || !line.startsWith("250")) {
133 PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
134 statmt.setString(1, address);
135 statmt.setString(2, "Failed to make a connection to the mail server");
136 statmt.setInt(3, forUid);
138 } catch (SQLException e) {