]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/EmailProvider.java
ADD: A step towards a more friendly SQL API.
[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                 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
81                     String line;
82                     while ((line = br.readLine()) != null && line.startsWith("220-")) {
83                     }
84                     if (line == null || !line.startsWith("220")) {
85                         continue;
86                     }
87
88                     pw.print("HELO www.cacert.org\r\n");
89                     pw.flush();
90
91                     while ((line = br.readLine()) != null && line.startsWith("220")) {
92                     }
93
94                     if (line == null || !line.startsWith("250")) {
95                         continue;
96                     }
97                     pw.print("MAIL FROM: <returns@cacert.org>\r\n");
98                     pw.flush();
99
100                     line = br.readLine();
101
102                     if (line == null || !line.startsWith("250")) {
103                         continue;
104                     }
105                     pw.print("RCPT TO: <" + address + ">\r\n");
106                     pw.flush();
107
108                     line = br.readLine();
109                     pw.print("QUIT\r\n");
110                     pw.flush();
111
112                     GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
113                     statmt.setString(1, address);
114                     statmt.setString(2, line);
115                     statmt.setInt(3, forUid);
116                     statmt.execute();
117
118                     if (line == null || !line.startsWith("250")) {
119                         return line;
120                     } else {
121                         return OK;
122                     }
123                 }
124
125             }
126         }
127         GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
128         statmt.setString(1, address);
129         statmt.setString(2, "Failed to make a connection to the mail server");
130         statmt.setInt(3, forUid);
131         statmt.execute();
132         return FAIL;
133     }
134
135 }