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