]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/EmailChecker.java
00c6738c24b7617a77aa5b829b407ca2e3fabfb2
[gigi.git] / src / org / cacert / gigi / util / EmailChecker.java
1 package org.cacert.gigi.util;
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.sql.PreparedStatement;
9 import java.sql.SQLException;
10 import java.util.LinkedList;
11 import java.util.regex.Pattern;
12
13 import org.cacert.gigi.database.DatabaseConnection;
14
15 public class EmailChecker {
16         public static final String OK = "OK";
17         public static final String FAIL = "FAIL";
18         private static final Pattern MAIL = Pattern
19                         .compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
20         private EmailChecker() {
21         }
22         public static String checkEmailServer(int forUid, String address)
23                         throws IOException {
24                 if (MAIL.matcher(address).matches()) {
25                         String[] parts = address.split("@", 2);
26                         String domain = parts[1];
27
28                         LinkedList<String> mxhosts = getMxHosts(domain);
29
30                         for (String host : mxhosts) {
31                                 try (Socket s = new Socket(host, 25);
32                                                 BufferedReader br = new BufferedReader(
33                                                                 new InputStreamReader(s.getInputStream()));
34                                                 PrintWriter pw = new PrintWriter(s.getOutputStream())) {
35                                         String line;
36                                         while ((line = br.readLine()).startsWith("220-")) {
37                                         }
38
39                                         if (!line.startsWith("220")) {
40                                                 continue;
41                                         }
42
43                                         pw.print("HELO www.cacert.org\r\n");
44                                         pw.flush();
45
46                                         while ((line = br.readLine()).startsWith("220")) {
47                                         }
48
49                                         if (!line.startsWith("250")) {
50                                                 continue;
51                                         }
52                                         pw.print("MAIL FROM: <returns@cacert.org>\r\n");
53                                         pw.flush();
54
55                                         line = br.readLine();
56
57                                         if (!line.startsWith("250")) {
58                                                 continue;
59                                         }
60                                         pw.print("RCPT TO: <" + address + ">\r\n");
61                                         pw.flush();
62
63                                         line = br.readLine();
64                                         pw.print("QUIT\r\n");
65                                         pw.flush();
66
67                                         try {
68                                                 PreparedStatement statmt = DatabaseConnection
69                                                                 .getInstance()
70                                                                 .prepare(
71                                                                                 "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
72                                                 statmt.setString(1, address);
73                                                 statmt.setString(2, line);
74                                                 statmt.setInt(3, forUid);
75                                                 statmt.execute();
76                                         } catch (SQLException e) {
77                                                 e.printStackTrace();
78                                         }
79
80                                         if (!line.startsWith("250")) {
81                                                 return line;
82                                         } else {
83                                                 return OK;
84                                         }
85                                 }
86
87                         }
88                 }
89                 try {
90                         PreparedStatement statmt = DatabaseConnection
91                                         .getInstance()
92                                         .prepare(
93                                                         "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
94                         statmt.setString(1, address);
95                         statmt.setString(2,
96                                         "Failed to make a connection to the mail server");
97                         statmt.setInt(3, forUid);
98                         statmt.execute();
99                 } catch (SQLException e) {
100                         e.printStackTrace();
101                 }
102                 return FAIL;
103         }
104         private static LinkedList<String> getMxHosts(String domain)
105                         throws IOException {
106                 LinkedList<String> mxhosts = new LinkedList<String>();
107                 Process dig = Runtime.getRuntime().exec(
108                                 new String[]{"dig", "+short", "MX", domain});
109                 try (BufferedReader br = new BufferedReader(new InputStreamReader(
110                                 dig.getInputStream()))) {
111                         String line;
112                         while ((line = br.readLine()) != null) {
113                                 String[] mxparts = line.split(" ", 2);
114                                 if (mxparts.length != 2) {
115                                         continue;
116                                 }
117                                 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));
118                         }
119                 }
120                 return mxhosts;
121         }
122
123 }