src/org/sonews/daemon/command/PostCommand.java
author František Kučera <franta-hg@frantovo.cz>
Sat Nov 05 00:06:09 2011 +0100 (2011-11-05)
changeset 115 e5bfc969d41f
parent 113 a059aecd1794
child 117 79ce65d63cce
permissions -rwxr-xr-x
SMTP: correct unescaping of posted messages containing lines with single dot.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package org.sonews.daemon.command;
    19 
    20 import java.io.IOException;
    21 import java.io.ByteArrayInputStream;
    22 import java.io.ByteArrayOutputStream;
    23 import java.sql.SQLException;
    24 import java.util.Arrays;
    25 import java.util.logging.Level;
    26 import javax.mail.MessagingException;
    27 import javax.mail.internet.AddressException;
    28 import javax.mail.internet.InternetHeaders;
    29 import org.sonews.config.Config;
    30 import org.sonews.util.Log;
    31 import org.sonews.mlgw.Dispatcher;
    32 import org.sonews.storage.Article;
    33 import org.sonews.storage.Group;
    34 import org.sonews.daemon.NNTPConnection;
    35 import org.sonews.storage.Headers;
    36 import org.sonews.storage.StorageBackendException;
    37 import org.sonews.storage.StorageManager;
    38 import org.sonews.feed.FeedManager;
    39 import org.sonews.util.Stats;
    40 import org.sonews.util.io.SMTPInputStream;
    41 
    42 /**
    43  * Implementation of the POST command. This command requires multiple lines
    44  * from the client, so the handling of asynchronous reading is a little tricky
    45  * to handle.
    46  * @author Christian Lins
    47  * @since sonews/0.5.0
    48  */
    49 public class PostCommand implements Command {
    50 
    51 	private final Article article = new Article();
    52 	private int lineCount = 0;
    53 	private long bodySize = 0;
    54 	private InternetHeaders headers = null;
    55 	private long maxBodySize =
    56 			Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
    57 	private PostState state = PostState.WaitForLineOne;
    58 	private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
    59 	private final StringBuilder strHead = new StringBuilder();
    60 
    61 	@Override
    62 	public String[] getSupportedCommandStrings() {
    63 		return new String[]{"POST"};
    64 	}
    65 
    66 	@Override
    67 	public boolean hasFinished() {
    68 		return this.state == PostState.Finished;
    69 	}
    70 
    71 	@Override
    72 	public String impliedCapability() {
    73 		return null;
    74 	}
    75 
    76 	@Override
    77 	public boolean isStateful() {
    78 		return true;
    79 	}
    80 
    81 	/**
    82 	 * Process the given line String. line.trim() was called by NNTPConnection.
    83 	 * @param line
    84 	 * @throws java.io.IOException
    85 	 * @throws java.sql.SQLException
    86 	 */
    87 	@Override // TODO: Refactor this method to reduce complexity!
    88 	public void processLine(NNTPConnection conn, String line, byte[] raw)
    89 			throws IOException, StorageBackendException {
    90 		switch (state) {
    91 			case WaitForLineOne: {
    92 				if (line.equalsIgnoreCase("POST")) {
    93 					conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
    94 					state = PostState.ReadingHeaders;
    95 				} else {
    96 					conn.println("500 invalid command usage");
    97 				}
    98 				break;
    99 			}
   100 			case ReadingHeaders: {
   101 				strHead.append(line);
   102 				strHead.append(NNTPConnection.NEWLINE);
   103 
   104 				if ("".equals(line) || ".".equals(line)) {
   105 					// we finally met the blank line
   106 					// separating headers from body
   107 					// WTF: "."
   108 
   109 					try {
   110 						// Parse the header using the InternetHeader class from JavaMail API
   111 						headers = new InternetHeaders(
   112 								new ByteArrayInputStream(strHead.toString().trim().getBytes(conn.getCurrentCharset())));
   113 
   114 						// add the header entries for the article
   115 						article.setHeaders(headers);
   116 					} catch (MessagingException ex) {
   117 						Log.get().log(Level.INFO, ex.getLocalizedMessage(), ex);
   118 						conn.println("500 posting failed - invalid header");
   119 						state = PostState.Finished;
   120 						break;
   121 					}
   122 
   123 					// Change charset for reading body;
   124 					// for multipart messages UTF-8 is returned
   125 					//conn.setCurrentCharset(article.getBodyCharset());
   126 
   127 					state = PostState.ReadingBody;
   128 
   129 					// WTF: do we need articles without bodies?
   130 					if (".".equals(line)) {
   131 						// Post an article without body
   132 						postArticle(conn, article);
   133 						state = PostState.Finished;
   134 					}
   135 				}
   136 				break;
   137 			}
   138 			case ReadingBody: {
   139 				if (".".equals(line)) {
   140 					// Set some headers needed for Over command
   141 					headers.setHeader(Headers.LINES, Integer.toString(lineCount));
   142 					headers.setHeader(Headers.BYTES, Long.toString(bodySize));
   143 
   144 					byte[] body = unescapeDots(bufBody.toByteArray());
   145 					if (body.length >= 2) {
   146 						// Remove trailing CRLF
   147 						body = Arrays.copyOf(body, body.length - 2);
   148 					}
   149 					article.setBody(body); // set the article body
   150 
   151 					postArticle(conn, article);
   152 					state = PostState.Finished;
   153 				} else {
   154 					bodySize += line.length() + 1;
   155 					lineCount++;
   156 
   157 					// Add line to body buffer
   158 					bufBody.write(raw, 0, raw.length);
   159 					bufBody.write(NNTPConnection.NEWLINE.getBytes());
   160 
   161 					if (bodySize > maxBodySize) {
   162 						conn.println("500 article is too long");
   163 						state = PostState.Finished;
   164 						break;
   165 					}
   166 				}
   167 				break;
   168 			}
   169 			default: {
   170 				// Should never happen
   171 				Log.get().severe("PostCommand::processLine(): already finished...");
   172 			}
   173 		}
   174 	}
   175 
   176 	/**
   177 	 * Article is a control message and needs special handling.
   178 	 * @param article
   179 	 */
   180 	private void controlMessage(NNTPConnection conn, Article article)
   181 			throws IOException {
   182 		String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
   183 		if (ctrl.length == 2) // "cancel <mid>"
   184 		{
   185 			try {
   186 				StorageManager.current().delete(ctrl[1]);
   187 
   188 				// Move cancel message to "control" group
   189 				article.setHeader(Headers.NEWSGROUPS, "control");
   190 				StorageManager.current().addArticle(article);
   191 				conn.println("240 article cancelled");
   192 			} catch (StorageBackendException ex) {
   193 				Log.get().severe(ex.toString());
   194 				conn.println("500 internal server error");
   195 			}
   196 		} else {
   197 			conn.println("441 unknown control header");
   198 		}
   199 	}
   200 
   201 	private void supersedeMessage(NNTPConnection conn, Article article)
   202 			throws IOException {
   203 		try {
   204 			String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
   205 			StorageManager.current().delete(oldMsg);
   206 			StorageManager.current().addArticle(article);
   207 			conn.println("240 article replaced");
   208 		} catch (StorageBackendException ex) {
   209 			Log.get().severe(ex.toString());
   210 			conn.println("500 internal server error");
   211 		}
   212 	}
   213 
   214 	private void postArticle(NNTPConnection conn, Article article)
   215 			throws IOException {
   216 		if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
   217 			article.setAuthenticatedUser(conn.getUser().getUserName());
   218 		}
   219 
   220 		if (article.getHeader(Headers.CONTROL)[0].length() > 0) {
   221 			controlMessage(conn, article);
   222 		} else if (article.getHeader(Headers.SUPERSEDES)[0].length() > 0) {
   223 			supersedeMessage(conn, article);
   224 		} else { // Post the article regularily
   225 			// Circle check; note that Path can already contain the hostname here
   226 			String host = Config.inst().get(Config.HOSTNAME, "localhost");
   227 			if (article.getHeader(Headers.PATH)[0].indexOf(host + "!", 1) > 0) {
   228 				Log.get().log(Level.INFO, "{0} skipped for host {1}", new Object[]{article.getMessageID(), host});
   229 				conn.println("441 I know this article already");
   230 				return;
   231 			}
   232 
   233 			// Try to create the article in the database or post it to
   234 			// appropriate mailing list
   235 			try {
   236 				boolean success = false;
   237 				String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
   238 				for (String groupname : groupnames) {
   239 					Group group = StorageManager.current().getGroup(groupname);
   240 					if (group != null && !group.isDeleted()) {
   241 						if (group.isMailingList() && !conn.isLocalConnection()) {
   242 							// Send to mailing list; the Dispatcher writes
   243 							// statistics to database
   244 							success = Dispatcher.toList(article, group.getName());
   245 						} else {
   246 							// Store in database
   247 							if (!StorageManager.current().isArticleExisting(article.getMessageID())) {
   248 								StorageManager.current().addArticle(article);
   249 
   250 								// Log this posting to statistics
   251 								Stats.getInstance().mailPosted(
   252 										article.getHeader(Headers.NEWSGROUPS)[0]);
   253 							}
   254 							success = true;
   255 						}
   256 					}
   257 				} // end for
   258 
   259 				if (success) {
   260 					conn.println("240 article posted ok");
   261 					FeedManager.queueForPush(article);
   262 				} else {
   263 					conn.println("441 newsgroup not found or configuration error");
   264 				}
   265 			} catch (AddressException ex) {
   266 				Log.get().warning(ex.getMessage());
   267 				conn.println("441 invalid sender address");
   268 			} catch (MessagingException ex) {
   269 				// A MessageException is thrown when the sender email address is
   270 				// invalid or something is wrong with the SMTP server.
   271 				System.err.println(ex.getLocalizedMessage());
   272 				conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
   273 			} catch (StorageBackendException ex) {
   274 				ex.printStackTrace();
   275 				conn.println("500 internal server error");
   276 			}
   277 		}
   278 	}
   279 
   280 	/**
   281 	 * TODO: rework, integrate into NNTPConnection
   282 	 * 
   283 	 * @param body message body with doubled dots
   284 	 * @return message body with unescaped dots (.. → .)
   285 	 */
   286 	private static byte[] unescapeDots(byte[] body) throws IOException {
   287 		byte[] result = new byte[body.length];
   288 		int resultLength = 0;
   289 
   290 		ByteArrayInputStream escapedInput = new ByteArrayInputStream(body);
   291 		SMTPInputStream unescapedInput = new SMTPInputStream(escapedInput);
   292 
   293 		int ch = unescapedInput.read();
   294 		while (ch >= 0) {
   295 			result[resultLength] = (byte) ch;
   296 			resultLength++;
   297 			ch = unescapedInput.read();
   298 		}
   299 
   300 		return Arrays.copyOfRange(result, 0, resultLength);
   301 	}
   302 }