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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2006-2008 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.IdentityManager;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import java.net.Socket;
  29. import java.io.BufferedReader;
  30. import java.io.InputStreamReader;
  31. import java.io.PrintWriter;
  32. import java.io.IOException;
  33. /**
  34. * The IdentClient responds to an ident request.
  35. *
  36. * @author Shane "Dataforce" Mc Cormack
  37. * @version $Id: IdentClient.java 969 2007-04-30 18:38:20Z ShaneMcC $
  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 = null;
  46. /**
  47. * Create the IdentClient
  48. *
  49. * @param server The server that owns this
  50. * @param socket The socket we are handing
  51. */
  52. public IdentClient(final IdentdServer server, final Socket socket) {
  53. myServer = server;
  54. mySocket = socket;
  55. myThread = new Thread(this);
  56. myThread.start();
  57. }
  58. /**
  59. * Process this connection
  60. */
  61. public void run() {
  62. final Thread thisThread = Thread.currentThread();
  63. PrintWriter out = null;
  64. BufferedReader in = null;
  65. try {
  66. out = new PrintWriter(mySocket.getOutputStream(), true);
  67. in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
  68. final String inputLine;
  69. if ((inputLine = in.readLine()) != null) {
  70. out.println(getIdentResponse(inputLine));
  71. }
  72. } catch (IOException e) {
  73. if (thisThread == myThread) {
  74. Logger.userError(ErrorLevel.HIGH ,"ClientSocket Error: "+e.getMessage());
  75. }
  76. } finally {
  77. try {
  78. out.close();
  79. in.close();
  80. mySocket.close();
  81. } catch (IOException e) { }
  82. }
  83. myServer.delClient(this);
  84. }
  85. /**
  86. * Get the ident response for a given line.
  87. * Complies with rfc1413 (http://www.faqs.org/rfcs/rfc1413.html)
  88. *
  89. * @param input Line to generate response for
  90. * @return the ident response for the given line
  91. */
  92. private String getIdentResponse(final String input) {
  93. final String[] bits = input.split(",", 2);
  94. if (bits.length < 2) {
  95. return String.format("%s : ERROR : X-INVALID-INPUT", input);
  96. }
  97. final int myPort;
  98. final int theirPort;
  99. try {
  100. myPort = Integer.parseInt(bits[0].trim());
  101. theirPort = Integer.parseInt(bits[1].trim());
  102. } catch (NumberFormatException e) {
  103. return String.format("%s : ERROR : X-INVALID-INPUT", input);
  104. }
  105. if (myPort > 65535 || myPort < 1 || theirPort > 65535 || theirPort < 1) {
  106. return String.format("%d , %d : ERROR : INVALID-PORT", myPort, theirPort);
  107. }
  108. final Server server = getServerByPort(myPort);
  109. if (!IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "advanced.alwaysOn")
  110. && (server == null || IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "advanced.isNoUser"))) {
  111. return String.format("%d , %d : ERROR : NO-USER", myPort, theirPort);
  112. }
  113. if (IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "advanced.isHiddenUser")) {
  114. return String.format("%d , %d : ERROR : HIDDEN-USER", myPort, theirPort);
  115. }
  116. final String osName = System.getProperty("os.name").toLowerCase();
  117. final String os;
  118. final String username;
  119. final String customSystem = IdentityManager.getGlobalConfig().getOption(IdentdPlugin.getDomain(), "advanced.customSystem");
  120. if (IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "advanced.useCustomSystem") && customSystem != null && customSystem.length() > 0 && customSystem.length() < 513) {
  121. os = customSystem;
  122. } else {
  123. // Tad excessive maybe, but complete!
  124. // Based on: http://mindprod.com/jgloss/properties.html
  125. // and the SYSTEM NAMES section of rfc1340 (http://www.faqs.org/rfcs/rfc1340.html)
  126. if (osName.startsWith("windows")) { os = "WIN32"; }
  127. else if (osName.startsWith("mac")) { os = "MACOS"; }
  128. else if (osName.startsWith("linux")) { os = "UNIX"; }
  129. else if (osName.indexOf("bsd") > -1) { os = "UNIX-BSD"; }
  130. else if ("os/2".equals(osName)) { os = "OS/2"; }
  131. else if (osName.indexOf("unix") > -1) { os = "UNIX"; }
  132. else if ("irix".equals(osName)) { os = "IRIX"; }
  133. else { os = "UNKNOWN"; }
  134. }
  135. final String customName = IdentityManager.getGlobalConfig().getOption(IdentdPlugin.getDomain(), "general.customName");
  136. if (IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "general.useCustomName") && customName != null && customName.length() > 0 && customName.length() < 513) {
  137. username = customName;
  138. } else if (server != null && IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "general.useNickname")) {
  139. username = server.getParser().getMyNickname();
  140. } else if (server != null && IdentityManager.getGlobalConfig().getOptionBool(IdentdPlugin.getDomain(), "general.useUsername")) {
  141. username = server.getParser().getMyUsername();
  142. } else {
  143. username = System.getProperty("user.name");
  144. }
  145. return String.format("%d , %d : USERID : %s : %s", myPort, theirPort, os, username);
  146. }
  147. /**
  148. * Close this IdentClient
  149. */
  150. public void close() {
  151. if (myThread != null) {
  152. final Thread tmpThread = myThread;
  153. myThread = null;
  154. if (tmpThread != null) { tmpThread.interrupt(); }
  155. try { mySocket.close(); } catch (IOException e) { }
  156. }
  157. }
  158. /**
  159. * Retrieves the server that is bound to the specified local port.
  160. *
  161. * @param port Port to check for
  162. * @return The server instance listening on the given port
  163. */
  164. private Server getServerByPort(final int port) {
  165. for (Server server : ServerManager.getServerManager().getServers()) {
  166. if (server.getParser().getLocalPort() == port) {
  167. return server;
  168. }
  169. }
  170. return null;
  171. }
  172. }