]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/EmailProvider.java
b67c44306e1a8af00dbf01de879ee10779e9d6cc
[gigi.git] / src / org / cacert / gigi / email / EmailProvider.java
1 package org.cacert.gigi.email;
2
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;
15
16 import javax.naming.NamingException;
17
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;
22
23 public abstract class EmailProvider {
24
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;
26
27     private static EmailProvider instance;
28
29     private X509Certificate c;
30
31     private PrivateKey k;
32
33     protected final void init(Certificate c, Key k) {
34         this.c = (X509Certificate) c;
35         this.k = (PrivateKey) k;
36     }
37
38     protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
39         SMIME.smime(contents, k, c, output);
40     }
41
42     public static EmailProvider getInstance() {
43         return instance;
44     }
45
46     protected static void setInstance(EmailProvider instance) {
47         EmailProvider.instance = instance;
48     }
49
50     public static void initSystem(Properties conf, Certificate cert, Key pk) {
51         try {
52             Class<?> c = Class.forName(conf.getProperty("emailProvider"));
53             EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
54             ep.init(cert, pk);
55             instance = ep;
56         } catch (ReflectiveOperationException e) {
57             e.printStackTrace();
58         }
59     }
60
61     public static final String OK = "OK";
62
63     public static final String FAIL = "FAIL";
64
65     public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
66
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];
71
72             String[] mxhosts;
73             try {
74                 mxhosts = DNSUtil.getMXEntries(domain);
75             } catch (NamingException e1) {
76                 return "MX lookup for your hostname failed.";
77             }
78
79             for (String host : mxhosts) {
80                 host = host.split(" ", 2)[1];
81                 if (host.endsWith(".")) {
82                     host = host.substring(0, host.length() - 1);
83                 } else {
84                     return "Strange MX records.";
85                 }
86                 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
87                     String line;
88                     while ((line = br.readLine()) != null && line.startsWith("220-")) {
89                     }
90                     if (line == null || !line.startsWith("220")) {
91                         continue;
92                     }
93
94                     pw.print("HELO www.cacert.org\r\n");
95                     pw.flush();
96
97                     while ((line = br.readLine()) != null && line.startsWith("220")) {
98                     }
99
100                     if (line == null || !line.startsWith("250")) {
101                         continue;
102                     }
103                     pw.print("MAIL FROM: <returns@cacert.org>\r\n");
104                     pw.flush();
105
106                     line = br.readLine();
107
108                     if (line == null || !line.startsWith("250")) {
109                         continue;
110                     }
111                     pw.print("RCPT TO: <" + address + ">\r\n");
112                     pw.flush();
113
114                     line = br.readLine();
115                     pw.print("QUIT\r\n");
116                     pw.flush();
117
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);
122                     statmt.execute();
123
124                     if (line == null || !line.startsWith("250")) {
125                         return line;
126                     } else {
127                         return OK;
128                     }
129                 }
130
131             }
132         }
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);
137         statmt.execute();
138         return FAIL;
139     }
140
141 }