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