]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/EmailProvider.java
Remove command invocations of "dig"
[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.sql.PreparedStatement;
14 import java.sql.SQLException;
15 import java.util.Properties;
16 import java.util.regex.Pattern;
17
18 import javax.naming.NamingException;
19
20 import org.cacert.gigi.crypto.SMIME;
21 import org.cacert.gigi.database.DatabaseConnection;
22 import org.cacert.gigi.util.DNSUtil;
23
24 public abstract class EmailProvider {
25
26     public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
27
28     private static EmailProvider instance;
29
30     private X509Certificate c;
31
32     private PrivateKey k;
33
34     protected final void init(Certificate c, Key k) {
35         this.c = (X509Certificate) c;
36         this.k = (PrivateKey) k;
37     }
38
39     protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
40         SMIME.smime(contents, k, c, output);
41     }
42
43     public static EmailProvider getInstance() {
44         return instance;
45     }
46
47     protected static void setInstance(EmailProvider instance) {
48         EmailProvider.instance = instance;
49     }
50
51     public static void initSystem(Properties conf, Certificate cert, Key pk) {
52         try {
53             Class<?> c = Class.forName(conf.getProperty("emailProvider"));
54             EmailProvider ep = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
55             ep.init(cert, pk);
56             instance = ep;
57         } catch (ReflectiveOperationException e) {
58             e.printStackTrace();
59         }
60     }
61
62     public static final String OK = "OK";
63
64     public static final String FAIL = "FAIL";
65
66     public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
67
68     public String checkEmailServer(int forUid, String address) throws IOException {
69         if (MAIL.matcher(address).matches()) {
70             String[] parts = address.split("@", 2);
71             String domain = parts[1];
72
73             String[] mxhosts;
74             try {
75                 mxhosts = DNSUtil.getMXEntries(domain);
76             } catch (NamingException e1) {
77                 return "MX lookup for your hostname failed.";
78             }
79
80             for (String host : mxhosts) {
81                 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
82                     String line;
83                     while ((line = br.readLine()) != null && line.startsWith("220-")) {
84                     }
85                     if (line == null || !line.startsWith("220")) {
86                         continue;
87                     }
88
89                     pw.print("HELO www.cacert.org\r\n");
90                     pw.flush();
91
92                     while ((line = br.readLine()) != null && line.startsWith("220")) {
93                     }
94
95                     if (line == null || !line.startsWith("250")) {
96                         continue;
97                     }
98                     pw.print("MAIL FROM: <returns@cacert.org>\r\n");
99                     pw.flush();
100
101                     line = br.readLine();
102
103                     if (line == null || !line.startsWith("250")) {
104                         continue;
105                     }
106                     pw.print("RCPT TO: <" + address + ">\r\n");
107                     pw.flush();
108
109                     line = br.readLine();
110                     pw.print("QUIT\r\n");
111                     pw.flush();
112
113                     try {
114                         PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
115                         statmt.setString(1, address);
116                         statmt.setString(2, line);
117                         statmt.setInt(3, forUid);
118                         statmt.execute();
119                     } catch (SQLException e) {
120                         e.printStackTrace();
121                     }
122
123                     if (line == null || !line.startsWith("250")) {
124                         return line;
125                     } else {
126                         return OK;
127                     }
128                 }
129
130             }
131         }
132         try {
133             PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` 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         } catch (SQLException e) {
139             e.printStackTrace();
140         }
141         return FAIL;
142     }
143
144 }