X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Femail%2FEmailProvider.java;h=a625e40193367b397a8f2c296385a6ec0dd79fad;hb=6d65334306c8b7bd10fbbfa07bc8f38475ff6d08;hp=b67c44306e1a8af00dbf01de879ee10779e9d6cc;hpb=05724453118769ed99b6021c602d9e987c97b9f3;p=gigi.git diff --git a/src/org/cacert/gigi/email/EmailProvider.java b/src/org/cacert/gigi/email/EmailProvider.java index b67c4430..a625e401 100644 --- a/src/org/cacert/gigi/email/EmailProvider.java +++ b/src/org/cacert/gigi/email/EmailProvider.java @@ -3,6 +3,7 @@ package org.cacert.gigi.email; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.security.GeneralSecurityException; @@ -10,10 +11,13 @@ import java.security.Key; import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.cert.X509Certificate; +import java.util.Arrays; +import java.util.Comparator; import java.util.Properties; import java.util.regex.Pattern; import javax.naming.NamingException; +import javax.net.ssl.SSLSocketFactory; import org.cacert.gigi.crypto.SMIME; import org.cacert.gigi.database.DatabaseConnection; @@ -75,6 +79,7 @@ public abstract class EmailProvider { } catch (NamingException e1) { return "MX lookup for your hostname failed."; } + sortMX(mxhosts); for (String host : mxhosts) { host = host.split(" ", 2)[1]; @@ -83,42 +88,68 @@ public abstract class EmailProvider { } else { return "Strange MX records."; } - try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) { + try (Socket s = new Socket(host, 25); BufferedReader br0 = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));// + PrintWriter pw0 = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"))) { + BufferedReader br = br0; + PrintWriter pw = pw0; String line; - while ((line = br.readLine()) != null && line.startsWith("220-")) { - } - if (line == null || !line.startsWith("220")) { + if ( !Sendmail.readSMTPResponse(br, 220)) { continue; } - pw.print("HELO www.cacert.org\r\n"); + pw.print("EHLO www.cacert.org\r\n"); pw.flush(); - - while ((line = br.readLine()) != null && line.startsWith("220")) { + boolean starttls = false; + do { + line = br.readLine(); + if (line == null) { + break; + } + starttls |= line.substring(4).equals("STARTTLS"); + } while (line.startsWith("250-")); + if (line == null || !line.startsWith("250 ")) { + continue; } - if (line == null || !line.startsWith("250")) { - continue; + if (starttls) { + pw.print("STARTTLS\r\n"); + pw.flush(); + if ( !Sendmail.readSMTPResponse(br, 220)) { + continue; + } + Socket s1 = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(s, host, 25, true); + br = new BufferedReader(new InputStreamReader(s1.getInputStream(), "UTF-8")); + pw = new PrintWriter(new OutputStreamWriter(s1.getOutputStream(), "UTF-8")); + pw.print("EHLO www.cacert.org\r\n"); + pw.flush(); + if ( !Sendmail.readSMTPResponse(br, 250)) { + continue; + } } + pw.print("MAIL FROM: \r\n"); pw.flush(); - line = br.readLine(); - - if (line == null || !line.startsWith("250")) { + if ( !Sendmail.readSMTPResponse(br, 250)) { continue; } pw.print("RCPT TO: <" + address + ">\r\n"); pw.flush(); - line = br.readLine(); + if ( !Sendmail.readSMTPResponse(br, 250)) { + continue; + } pw.print("QUIT\r\n"); pw.flush(); + if ( !Sendmail.readSMTPResponse(br, 221)) { + continue; + } - GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `emailPinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?"); + GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`=?::`pingState`"); statmt.setString(1, address); statmt.setString(2, line); statmt.setInt(3, forUid); + statmt.setString(4, "success"); statmt.execute(); if (line == null || !line.startsWith("250")) { @@ -130,12 +161,25 @@ public abstract class EmailProvider { } } - GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `emailPinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?"); + GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`=?::`pingState`"); statmt.setString(1, address); statmt.setString(2, "Failed to make a connection to the mail server"); statmt.setInt(3, forUid); + statmt.setString(4, "failed"); statmt.execute(); return FAIL; } + private static void sortMX(String[] mxhosts) { + Arrays.sort(mxhosts, new Comparator() { + + @Override + public int compare(String o1, String o2) { + int i1 = Integer.parseInt(o1.split(" ")[0]); + int i2 = Integer.parseInt(o2.split(" ")[0]); + return Integer.compare(i1, i2); + } + }); + } + }