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