📄 smtphandler.java
字号:
.append(" [") .append(remoteIP) .append("])"); responseString = clearResponseBuffer(); if (authRequired) { writeLoggedResponse(responseString); responseString = "250-AUTH LOGIN PLAIN"; writeLoggedResponse(responseString); responseString = "250 AUTH=LOGIN PLAIN"; } writeLoggedFlushedResponse(responseString); } } /** * Handler method called upon receipt of a EHLO command. * Responds with a greeting and informs the client whether * client authentication is required. * * @param argument the argument passed in with the command by the SMTP client */ private void doEHLO(String argument) { String responseString = null; if (argument == null) { responseString = "501 Domain address required: " + COMMAND_EHLO; writeLoggedFlushedResponse(responseString); } else { resetState(); state.put(CURRENT_HELO_MODE, COMMAND_EHLO); // Extension defined in RFC 1870 long maxMessageSize = theConfigData.getMaxMessageSize(); if (maxMessageSize > 0) { responseString = "250-SIZE " + maxMessageSize; writeLoggedResponse(responseString); } if (authRequired) { //This is necessary because we're going to do a multiline response responseBuffer.append("250-"); } else { responseBuffer.append("250 "); } responseBuffer.append(theConfigData.getHelloName()) .append(" Hello ") .append(argument) .append(" (") .append(remoteHost) .append(" [") .append(remoteIP) .append("])"); responseString = clearResponseBuffer(); if (authRequired) { writeLoggedResponse(responseString); responseString = "250-AUTH LOGIN PLAIN"; writeLoggedResponse(responseString); responseString = "250 AUTH=LOGIN PLAIN"; } writeLoggedFlushedResponse(responseString); } } /** * Handler method called upon receipt of a AUTH command. * Handles client authentication to the SMTP server. * * @param argument the argument passed in with the command by the SMTP client */ private void doAUTH(String argument) throws Exception { String responseString = null; if (getUser() != null) { responseString = "503 User has previously authenticated. " + " Further authentication is not required!"; writeLoggedFlushedResponse(responseString); } else if (argument == null) { responseString = "501 Usage: AUTH (authentication type) <challenge>"; writeLoggedFlushedResponse(responseString); } else { String initialResponse = null; if ((argument != null) && (argument.indexOf(" ") > 0)) { initialResponse = argument.substring(argument.indexOf(" ") + 1); argument = argument.substring(0,argument.indexOf(" ")); } String authType = argument.toUpperCase(Locale.US); if (authType.equals(AUTH_TYPE_PLAIN)) { doPlainAuth(initialResponse); return; } else if (authType.equals(AUTH_TYPE_LOGIN)) { doLoginAuth(initialResponse); return; } else { doUnknownAuth(authType, initialResponse); return; } } } /** * Carries out the Plain AUTH SASL exchange. * * According to RFC 2595 the client must send: [authorize-id] \0 authenticate-id \0 password. * * >>> AUTH PLAIN dGVzdAB0ZXN0QHdpei5leGFtcGxlLmNvbQB0RXN0NDI= * Decoded: test\000test@wiz.example.com\000tEst42 * * >>> AUTH PLAIN dGVzdAB0ZXN0AHRFc3Q0Mg== * Decoded: test\000test\000tEst42 * * @param initialResponse the initial response line passed in with the AUTH command */ private void doPlainAuth(String initialResponse) throws IOException { String userpass = null, user = null, pass = null, responseString = null; if (initialResponse == null) { responseString = "334 OK. Continue authentication"; writeLoggedFlushedResponse(responseString); userpass = readCommandLine(); } else { userpass = initialResponse.trim(); } try { if (userpass != null) { userpass = Base64.decodeAsString(userpass); } if (userpass != null) { /* See: RFC 2595, Section 6 The mechanism consists of a single message from the client to the server. The client sends the authorization identity (identity to login as), followed by a US-ASCII NUL character, followed by the authentication identity (identity whose password will be used), followed by a US-ASCII NUL character, followed by the clear-text password. The client may leave the authorization identity empty to indicate that it is the same as the authentication identity. The server will verify the authentication identity and password with the system authentication database and verify that the authentication credentials permit the client to login as the authorization identity. If both steps succeed, the user is logged in. */ StringTokenizer authTokenizer = new StringTokenizer(userpass, "\0"); String authorize_id = authTokenizer.nextToken(); // Authorization Identity user = authTokenizer.nextToken(); // Authentication Identity try { pass = authTokenizer.nextToken(); // Password } catch (java.util.NoSuchElementException _) { // If we got here, this is what happened. RFC 2595 // says that "the client may leave the authorization // identity empty to indicate that it is the same as // the authentication identity." As noted above, // that would be represented as a decoded string of // the form: "\0authenticate-id\0password". The // first call to nextToken will skip the empty // authorize-id, and give us the authenticate-id, // which we would store as the authorize-id. The // second call will give us the password, which we // think is the authenticate-id (user). Then when // we ask for the password, there are no more // elements, leading to the exception we just // caught. So we need to move the user to the // password, and the authorize_id to the user. pass = user; user = authorize_id; } authTokenizer = null; } } catch (Exception e) { // Ignored - this exception in parsing will be dealt // with in the if clause below } // Authenticate user if ((user == null) || (pass == null)) { responseString = "501 Could not decode parameters for AUTH PLAIN"; writeLoggedFlushedResponse(responseString); } else if (theConfigData.getUsersRepository().test(user, pass)) { setUser(user); responseString = "235 Authentication Successful"; writeLoggedFlushedResponse(responseString); getLogger().info("AUTH method PLAIN succeeded"); } else { responseString = "535 Authentication Failed"; writeLoggedFlushedResponse(responseString); getLogger().error("AUTH method PLAIN failed"); } return; } /** * Carries out the Login AUTH SASL exchange. * * @param initialResponse the initial response line passed in with the AUTH command */ private void doLoginAuth(String initialResponse) throws IOException { String user = null, pass = null, responseString = null; if (initialResponse == null) { responseString = "334 VXNlcm5hbWU6"; // base64 encoded "Username:" writeLoggedFlushedResponse(responseString); user = readCommandLine(); } else { user = initialResponse.trim(); } if (user != null) { try { user = Base64.decodeAsString(user); } catch (Exception e) { // Ignored - this parse error will be // addressed in the if clause below user = null; } } responseString = "334 UGFzc3dvcmQ6"; // base64 encoded "Password:" writeLoggedFlushedResponse(responseString); pass = readCommandLine(); if (pass != null) { try { pass = Base64.decodeAsString(pass); } catch (Exception e) { // Ignored - this parse error will be // addressed in the if clause below pass = null; } } // Authenticate user if ((user == null) || (pass == null)) { responseString = "501 Could not decode parameters for AUTH LOGIN"; } else if (theConfigData.getUsersRepository().test(user, pass)) { setUser(user); responseString = "235 Authentication Successful"; if (getLogger().isDebugEnabled()) { // TODO: Make this string a more useful debug message getLogger().debug("AUTH method LOGIN succeeded"); } } else { responseString = "535 Authentication Failed"; // TODO: Make this string a more useful error message getLogger().error("AUTH method LOGIN failed"); } writeLoggedFlushedResponse(responseString); return; } /** * Handles the case of an unrecognized auth type. * * @param authType the unknown auth type * @param initialResponse the initial response line passed in with the AUTH command */ private void doUnknownAuth(String authType, String initialResponse) { String responseString = "504 Unrecognized Authentication Type"; writeLoggedFlushedResponse(responseString); if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(128) .append("AUTH method ") .append(authType) .append(" is an unrecognized authentication type"); getLogger().error(errorBuffer.toString()); } return; } /** * Handler method called upon receipt of a MAIL command. * Sets up handler to deliver mail as the stated sender. * * @param argument the argument passed in with the command by the SMTP client */ private void doMAIL(String argument) { String responseString = null; String sender = null; if ((argument != null) && (argument.indexOf(":") > 0)) { int colonIndex = argument.indexOf(":"); sender = argument.substring(colonIndex + 1); argument = argument.substring(0, colonIndex); } if (state.containsKey(SENDER)) { responseString = "503 Sender already specified"; writeLoggedFlushedResponse(responseString); } else if (argument == null || !argument.toUpperCase(Locale.US).equals("FROM") || sender == null) { responseString = "501 Usage: MAIL FROM:<sender>"; writeLoggedFlushedResponse(responseString); } else { sender = sender.trim(); // the next gt after the first lt ... AUTH may add more <> int lastChar = sender.indexOf('>', sender.indexOf('<')); // Check to see if any options are present and, if so, whether they are correctly formatted // (separated from the closing angle bracket by a ' '). if ((lastChar > 0) && (sender.length() > lastChar + 2) && (sender.charAt(lastChar + 1) == ' ')) { String mailOptionString = sender.substring(lastChar + 2); // Remove the options from the sender sender = sender.substring(0, lastChar + 1); StringTokenizer optionTokenizer = new StringTokenizer(mailOptionString, " "); while (optionTokenizer.hasMoreElements()) { String mailOption = optionTokenizer.nextToken(); int equalIndex = mailOptionString.indexOf('='); String mailOptionName = mailOption; String mailOptionValue = ""; if (equalIndex > 0) { mailOptionName = mailOption.substring(0, equalIndex).toUpperCase(Locale.US); mailOptionValue = mailOption.substring(equalIndex + 1); } // Handle the SIZE extension keyword if (mailOptionName.startsWith(MAIL_OPTION_SIZE)) { if (!(doMailSize(mailOptionValue))) { return; } } else { // Unexpected option attached to the Mail command
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -