You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IdentClient.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.identd;
  23. import com.dmdirc.Server;
  24. import com.dmdirc.ServerManager;
  25. import com.dmdirc.config.ConfigManager;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.logger.Logger;
  29. import java.io.BufferedReader;
  30. import java.io.InputStreamReader;
  31. import java.io.IOException;
  32. import java.io.PrintWriter;
  33. import java.net.Socket;
  34. /**
  35. * The IdentClient responds to an ident request.
  36. *
  37. * @author Shane "Dataforce" Mc Cormack
  38. */
  39. public final class IdentClient implements Runnable {
  40. /** The IdentdServer that owns this Client. */
  41. private final IdentdServer myServer;
  42. /** The Socket that we are in charge of. */
  43. private final Socket mySocket;
  44. /** The Thread in use for this client. */
  45. private volatile Thread myThread;
  46. /** The plugin that owns us. */
  47. private final IdentdPlugin myPlugin;
  48. /**
  49. * Create the IdentClient.
  50. *
  51. * @param server The server that owns this
  52. * @param socket The socket we are handing
  53. */
  54. public IdentClient(final IdentdServer server, final Socket socket, final IdentdPlugin plugin) {
  55. myServer = server;
  56. mySocket = socket;
  57. myPlugin = plugin;
  58. myThread = new Thread(this);
  59. myThread.start();
  60. }
  61. /**
  62. * Process this connection.
  63. */
  64. @Override
  65. public void run() {
  66. final Thread thisThread = Thread.currentThread();
  67. PrintWriter out = null;
  68. BufferedReader in = null;
  69. try {
  70. out = new PrintWriter(mySocket.getOutputStream(), true);
  71. in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
  72. final String inputLine;
  73. if ((inputLine = in.readLine()) != null) {
  74. out.println(getIdentResponse(inputLine, IdentityManager.getGlobalConfig()));
  75. }
  76. } catch (IOException e) {
  77. if (thisThread == myThread) {
  78. Logger.userError(ErrorLevel.HIGH, "ClientSocket Error: " + e.getMessage());
  79. }
  80. } finally {
  81. try {
  82. out.close();
  83. in.close();
  84. mySocket.close();
  85. } catch (IOException e) {
  86. }
  87. }
  88. myServer.delClient(this);
  89. }
  90. /**
  91. * Get the ident response for a given line.
  92. * Complies with rfc1413 (http://www.faqs.org/rfcs/rfc1413.html)
  93. *
  94. * @param input Line to generate response for
  95. * @param config The config manager to use for settings
  96. * @return the ident response for the given line
  97. */
  98. protected String getIdentResponse(final String input, final ConfigManager config) {
  99. final String unescapedInput = unescapeString(input);
  100. final String[] bits = unescapedInput.replaceAll("\\s+", "").split(",", 2);
  101. if (bits.length < 2) {
  102. return String.format("%s : ERROR : X-INVALID-INPUT", escapeString(unescapedInput));
  103. }
  104. final int myPort;
  105. final int theirPort;
  106. try {
  107. myPort = Integer.parseInt(bits[0].trim());
  108. theirPort = Integer.parseInt(bits[1].trim());
  109. } catch (NumberFormatException e) {
  110. return String.format("%s , %s : ERROR : X-INVALID-INPUT", escapeString(bits[0]), escapeString(bits[1]));
  111. }
  112. if (myPort > 65535 || myPort < 1 || theirPort > 65535 || theirPort < 1) {
  113. return String.format("%d , %d : ERROR : INVALID-PORT", myPort, theirPort);
  114. }
  115. final Server server = getServerByPort(myPort);
  116. if (!config.getOptionBool(myPlugin.getDomain(), "advanced.alwaysOn") && (server == null || config.getOptionBool(myPlugin.getDomain(), "advanced.isNoUser"))) {
  117. return String.format("%d , %d : ERROR : NO-USER", myPort, theirPort);
  118. }
  119. if (config.getOptionBool(myPlugin.getDomain(), "advanced.isHiddenUser")) {
  120. return String.format("%d , %d : ERROR : HIDDEN-USER", myPort, theirPort);
  121. }
  122. final String osName = System.getProperty("os.name").toLowerCase();
  123. final String os;
  124. final String username;
  125. final String customSystem = config.getOption(myPlugin.getDomain(), "advanced.customSystem");
  126. if (config.getOptionBool(myPlugin.getDomain(), "advanced.useCustomSystem") && customSystem != null && customSystem.length() > 0 && customSystem.length() < 513) {
  127. os = customSystem;
  128. } else {
  129. // Tad excessive maybe, but complete!
  130. // Based on: http://mindprod.com/jgloss/properties.html
  131. // and the SYSTEM NAMES section of rfc1340 (http://www.faqs.org/rfcs/rfc1340.html)
  132. if (osName.startsWith("windows")) {
  133. os = "WIN32";
  134. } else if (osName.startsWith("mac")) {
  135. os = "MACOS";
  136. } else if (osName.startsWith("linux")) {
  137. os = "UNIX";
  138. } else if (osName.indexOf("bsd") > -1) {
  139. os = "UNIX-BSD";
  140. } else if ("os/2".equals(osName)) {
  141. os = "OS/2";
  142. } else if (osName.indexOf("unix") > -1) {
  143. os = "UNIX";
  144. } else if ("irix".equals(osName)) {
  145. os = "IRIX";
  146. } else {
  147. os = "UNKNOWN";
  148. }
  149. }
  150. final String customName = config.getOption(myPlugin.getDomain(), "general.customName");
  151. if (config.getOptionBool(myPlugin.getDomain(), "general.useCustomName") && customName != null && customName.length() > 0 && customName.length() < 513) {
  152. username = customName;
  153. } else if (server != null && config.getOptionBool(myPlugin.getDomain(), "general.useNickname")) {
  154. username = server.getParser().getLocalClient().getNickname();
  155. } else if (server != null && config.getOptionBool(myPlugin.getDomain(), "general.useUsername")) {
  156. username = server.getParser().getLocalClient().getUsername();
  157. } else {
  158. username = System.getProperty("user.name");
  159. }
  160. return String.format("%d , %d : USERID : %s : %s", myPort, theirPort, escapeString(os), escapeString(username));
  161. }
  162. /**
  163. * Escape special chars.
  164. *
  165. * @param str String to escape
  166. * @return Escaped string.
  167. */
  168. public static String escapeString(final String str) {
  169. return str.replaceAll("\\\\", "\\\\\\\\").replaceAll(":", "\\\\:").replaceAll(",", "\\\\,").replaceAll(" ", "\\\\ ");
  170. }
  171. /**
  172. * Unescape special chars.
  173. *
  174. * @param str String to escape
  175. * @return Escaped string.
  176. */
  177. public static String unescapeString(final String str) {
  178. return str.replaceAll("\\\\:", ":").replaceAll("\\\\ ", " ").replaceAll("\\\\,", ",").replaceAll("\\\\\\\\", "\\\\");
  179. }
  180. /**
  181. * Close this IdentClient.
  182. */
  183. public void close() {
  184. if (myThread != null) {
  185. final Thread tmpThread = myThread;
  186. myThread = null;
  187. if (tmpThread != null) {
  188. tmpThread.interrupt();
  189. }
  190. try {
  191. mySocket.close();
  192. } catch (IOException e) {
  193. }
  194. }
  195. }
  196. /**
  197. * Retrieves the server that is bound to the specified local port.
  198. *
  199. * @param port Port to check for
  200. * @return The server instance listening on the given port
  201. */
  202. protected static Server getServerByPort(final int port) {
  203. for (Server server : ServerManager.getServerManager().getServers()) {
  204. if (server.getParser().getLocalPort() == port) {
  205. return server;
  206. }
  207. }
  208. return null;
  209. }
  210. }