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.util.Properties;
14 import java.util.regex.Pattern;
16 import javax.naming.NamingException;
18 import org.cacert.gigi.crypto.SMIME;
19 import org.cacert.gigi.database.DatabaseConnection;
20 import org.cacert.gigi.database.GigiPreparedStatement;
21 import org.cacert.gigi.util.DNSUtil;
23 public abstract class EmailProvider {
25 public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
27 private static EmailProvider instance;
29 private X509Certificate c;
33 protected final void init(Certificate c, Key k) {
34 this.c = (X509Certificate) c;
35 this.k = (PrivateKey) k;
38 protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
39 SMIME.smime(contents, k, c, output);
42 public static EmailProvider getInstance() {
46 protected static void setInstance(EmailProvider instance) {
47 EmailProvider.instance = instance;
50 public static void initSystem(Properties conf, Certificate cert, Key pk) {
52 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
53 EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
56 } catch (ReflectiveOperationException e) {
61 public static final String OK = "OK";
63 public static final String FAIL = "FAIL";
65 public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
67 public String checkEmailServer(int forUid, String address) throws IOException {
68 if (MAIL.matcher(address).matches()) {
69 String[] parts = address.split("@", 2);
70 String domain = parts[1];
74 mxhosts = DNSUtil.getMXEntries(domain);
75 } catch (NamingException e1) {
76 return "MX lookup for your hostname failed.";
79 for (String host : mxhosts) {
80 host = host.split(" ", 2)[1];
81 if (host.endsWith(".")) {
82 host = host.substring(0, host.length() - 1);
84 return "Strange MX records.";
86 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
88 while ((line = br.readLine()) != null && line.startsWith("220-")) {
90 if (line == null || !line.startsWith("220")) {
94 pw.print("HELO www.cacert.org\r\n");
97 while ((line = br.readLine()) != null && line.startsWith("220")) {
100 if (line == null || !line.startsWith("250")) {
103 pw.print("MAIL FROM: <returns@cacert.org>\r\n");
106 line = br.readLine();
108 if (line == null || !line.startsWith("250")) {
111 pw.print("RCPT TO: <" + address + ">\r\n");
114 line = br.readLine();
115 pw.print("QUIT\r\n");
118 GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `emailPinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
119 statmt.setString(1, address);
120 statmt.setString(2, line);
121 statmt.setInt(3, forUid);
124 if (line == null || !line.startsWith("250")) {
133 GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `emailPinglog` 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);