]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/EmailProvider.java
add: devel-convenience TestEmailProvider can have a secondary mail
[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.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;
18
19 import javax.naming.NamingException;
20 import javax.net.ssl.SSLSocketFactory;
21
22 import org.cacert.gigi.crypto.SMIME;
23 import org.cacert.gigi.database.DatabaseConnection;
24 import org.cacert.gigi.database.GigiPreparedStatement;
25 import org.cacert.gigi.util.DNSUtil;
26
27 public abstract class EmailProvider {
28
29     public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
30
31     private static EmailProvider instance;
32
33     private X509Certificate c;
34
35     private PrivateKey k;
36
37     protected void init(Certificate c, Key k) {
38         this.c = (X509Certificate) c;
39         this.k = (PrivateKey) k;
40     }
41
42     protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
43         SMIME.smime(contents, k, c, output);
44     }
45
46     public static EmailProvider getInstance() {
47         return instance;
48     }
49
50     protected static void setInstance(EmailProvider instance) {
51         EmailProvider.instance = instance;
52     }
53
54     public static void initSystem(Properties conf, Certificate cert, Key pk) {
55         try {
56             Class<?> c = Class.forName(conf.getProperty("emailProvider"));
57             EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
58             ep.init(cert, pk);
59             instance = ep;
60         } catch (ReflectiveOperationException e) {
61             e.printStackTrace();
62         }
63     }
64
65     public static final String OK = "OK";
66
67     public static final String FAIL = "FAIL";
68
69     public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
70
71     public String checkEmailServer(int forUid, String address) throws IOException {
72         if (MAIL.matcher(address).matches()) {
73             String[] parts = address.split("@", 2);
74             String domain = parts[1];
75
76             String[] mxhosts;
77             try {
78                 mxhosts = DNSUtil.getMXEntries(domain);
79             } catch (NamingException e1) {
80                 return "MX lookup for your hostname failed.";
81             }
82             sortMX(mxhosts);
83
84             for (String host : mxhosts) {
85                 host = host.split(" ", 2)[1];
86                 if (host.endsWith(".")) {
87                     host = host.substring(0, host.length() - 1);
88                 } else {
89                     return "Strange MX records.";
90                 }
91                 try (Socket s = new Socket(host, 25); BufferedReader br0 = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));//
92                         PrintWriter pw0 = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"))) {
93                     BufferedReader br = br0;
94                     PrintWriter pw = pw0;
95                     String line;
96                     if ( !Sendmail.readSMTPResponse(br, 220)) {
97                         continue;
98                     }
99
100                     pw.print("EHLO www.cacert.org\r\n");
101                     pw.flush();
102                     boolean starttls = false;
103                     do {
104                         line = br.readLine();
105                         if (line == null) {
106                             break;
107                         }
108                         starttls |= line.substring(4).equals("STARTTLS");
109                     } while (line.startsWith("250-"));
110                     if (line == null || !line.startsWith("250 ")) {
111                         continue;
112                     }
113
114                     if (starttls) {
115                         pw.print("STARTTLS\r\n");
116                         pw.flush();
117                         if ( !Sendmail.readSMTPResponse(br, 220)) {
118                             continue;
119                         }
120                         Socket s1 = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(s, host, 25, true);
121                         br = new BufferedReader(new InputStreamReader(s1.getInputStream(), "UTF-8"));
122                         pw = new PrintWriter(new OutputStreamWriter(s1.getOutputStream(), "UTF-8"));
123                         pw.print("EHLO www.cacert.org\r\n");
124                         pw.flush();
125                         if ( !Sendmail.readSMTPResponse(br, 250)) {
126                             continue;
127                         }
128                     }
129
130                     pw.print("MAIL FROM: <returns@cacert.org>\r\n");
131                     pw.flush();
132
133                     if ( !Sendmail.readSMTPResponse(br, 250)) {
134                         continue;
135                     }
136                     pw.print("RCPT TO: <" + address + ">\r\n");
137                     pw.flush();
138
139                     if ( !Sendmail.readSMTPResponse(br, 250)) {
140                         continue;
141                     }
142                     pw.print("QUIT\r\n");
143                     pw.flush();
144                     if ( !Sendmail.readSMTPResponse(br, 221)) {
145                         continue;
146                     }
147
148                     GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`=?::`pingState`");
149                     statmt.setString(1, address);
150                     statmt.setString(2, line);
151                     statmt.setInt(3, forUid);
152                     statmt.setString(4, "success");
153                     statmt.execute();
154
155                     if (line == null || !line.startsWith("250")) {
156                         return line;
157                     } else {
158                         return OK;
159                     }
160                 }
161
162             }
163         }
164         GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`=?::`pingState`");
165         statmt.setString(1, address);
166         statmt.setString(2, "Failed to make a connection to the mail server");
167         statmt.setInt(3, forUid);
168         statmt.setString(4, "failed");
169         statmt.execute();
170         return FAIL;
171     }
172
173     private static void sortMX(String[] mxhosts) {
174         Arrays.sort(mxhosts, new Comparator<String>() {
175
176             @Override
177             public int compare(String o1, String o2) {
178                 int i1 = Integer.parseInt(o1.split(" ")[0]);
179                 int i2 = Integer.parseInt(o2.split(" ")[0]);
180                 return Integer.compare(i1, i2);
181             }
182         });
183     }
184
185 }