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.LinkedList;
16 import java.util.Properties;
17 import java.util.regex.Pattern;
19 import org.cacert.gigi.crypto.SMIME;
20 import org.cacert.gigi.database.DatabaseConnection;
22 public abstract class EmailProvider {
24 public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
26 private static EmailProvider instance;
28 private X509Certificate c;
32 protected final void init(Certificate c, Key k) {
33 this.c = (X509Certificate) c;
34 this.k = (PrivateKey) k;
37 protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
38 SMIME.smime(contents, k, c, output);
41 public static EmailProvider getInstance() {
45 protected static void setInstance(EmailProvider instance) {
46 EmailProvider.instance = instance;
49 public static void initSystem(Properties conf, Certificate cert, Key pk) {
51 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
52 EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
55 } catch (ReflectiveOperationException e) {
60 public static final String OK = "OK";
62 public static final String FAIL = "FAIL";
64 public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
66 public String checkEmailServer(int forUid, String address) throws IOException {
67 if (MAIL.matcher(address).matches()) {
68 String[] parts = address.split("@", 2);
69 String domain = parts[1];
71 LinkedList<String> mxhosts = getMxHosts(domain);
73 for (String host : mxhosts) {
74 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
76 while ((line = br.readLine()) != null && line.startsWith("220-")) {
78 if (line == null || !line.startsWith("220")) {
82 pw.print("HELO www.cacert.org\r\n");
85 while ((line = br.readLine()) != null && line.startsWith("220")) {
88 if (line == null || !line.startsWith("250")) {
91 pw.print("MAIL FROM: <returns@cacert.org>\r\n");
96 if (line == null || !line.startsWith("250")) {
99 pw.print("RCPT TO: <" + address + ">\r\n");
102 line = br.readLine();
103 pw.print("QUIT\r\n");
107 PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
108 statmt.setString(1, address);
109 statmt.setString(2, line);
110 statmt.setInt(3, forUid);
112 } catch (SQLException e) {
116 if (line == null || !line.startsWith("250")) {
126 PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
127 statmt.setString(1, address);
128 statmt.setString(2, "Failed to make a connection to the mail server");
129 statmt.setInt(3, forUid);
131 } catch (SQLException e) {
137 private static LinkedList<String> getMxHosts(String domain) throws IOException {
138 LinkedList<String> mxhosts = new LinkedList<String>();
139 Process dig = Runtime.getRuntime().exec(new String[] {
140 "dig", "+short", "MX", domain
142 try (BufferedReader br = new BufferedReader(new InputStreamReader(dig.getInputStream()))) {
144 while ((line = br.readLine()) != null) {
145 String[] mxparts = line.split(" ", 2);
146 if (mxparts.length != 2) {
149 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));