]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/EmailChecker.java
Fix a "FindBug" in "EmailChecker"
[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()) != null
37                                                         && line.startsWith("220-")) {
38                                         }
39                                         if (line == null || !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()) != null
47                                                         && line.startsWith("220")) {
48                                         }
49
50                                         if (line == null || !line.startsWith("250")) {
51                                                 continue;
52                                         }
53                                         pw.print("MAIL FROM: <returns@cacert.org>\r\n");
54                                         pw.flush();
55
56                                         line = br.readLine();
57
58                                         if (line == null || !line.startsWith("250")) {
59                                                 continue;
60                                         }
61                                         pw.print("RCPT TO: <" + address + ">\r\n");
62                                         pw.flush();
63
64                                         line = br.readLine();
65                                         pw.print("QUIT\r\n");
66                                         pw.flush();
67
68                                         try {
69                                                 PreparedStatement statmt = DatabaseConnection
70                                                                 .getInstance()
71                                                                 .prepare(
72                                                                                 "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
73                                                 statmt.setString(1, address);
74                                                 statmt.setString(2, line);
75                                                 statmt.setInt(3, forUid);
76                                                 statmt.execute();
77                                         } catch (SQLException e) {
78                                                 e.printStackTrace();
79                                         }
80
81                                         if (line == null || !line.startsWith("250")) {
82                                                 return line;
83                                         } else {
84                                                 return OK;
85                                         }
86                                 }
87
88                         }
89                 }
90                 try {
91                         PreparedStatement statmt = DatabaseConnection
92                                         .getInstance()
93                                         .prepare(
94                                                         "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
95                         statmt.setString(1, address);
96                         statmt.setString(2,
97                                         "Failed to make a connection to the mail server");
98                         statmt.setInt(3, forUid);
99                         statmt.execute();
100                 } catch (SQLException e) {
101                         e.printStackTrace();
102                 }
103                 return FAIL;
104         }
105         private static LinkedList<String> getMxHosts(String domain)
106                         throws IOException {
107                 LinkedList<String> mxhosts = new LinkedList<String>();
108                 Process dig = Runtime.getRuntime().exec(
109                                 new String[]{"dig", "+short", "MX", domain});
110                 try (BufferedReader br = new BufferedReader(new InputStreamReader(
111                                 dig.getInputStream()))) {
112                         String line;
113                         while ((line = br.readLine()) != null) {
114                                 String[] mxparts = line.split(" ", 2);
115                                 if (mxparts.length != 2) {
116                                         continue;
117                                 }
118                                 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));
119                         }
120                 }
121                 return mxhosts;
122         }
123
124 }