PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/opennms-services/src/main/java/org/opennms/netmgt/capsd/SmbUtils.java

https://github.com/DerTak/opennms
Java | 413 lines | 121 code | 59 blank | 233 comment | 49 complexity | aea8e86a79a61fce64ffdccf2b638bf3 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0
  1. /*******************************************************************************
  2. * This file is part of OpenNMS(R).
  3. *
  4. * Copyright (C) 2006-2012 The OpenNMS Group, Inc.
  5. * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
  6. *
  7. * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
  8. *
  9. * OpenNMS(R) is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published
  11. * by the Free Software Foundation, either version 3 of the License,
  12. * or (at your option) any later version.
  13. *
  14. * OpenNMS(R) is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with OpenNMS(R). If not, see:
  21. * http://www.gnu.org/licenses/
  22. *
  23. * For more information contact:
  24. * OpenNMS(R) Licensing <license@opennms.org>
  25. * http://www.opennms.org/
  26. * http://www.opennms.com/
  27. *******************************************************************************/
  28. package org.opennms.netmgt.capsd;
  29. import jcifs.netbios.NbtAddress;
  30. import org.opennms.netmgt.config.capsd.SmbAuth;
  31. /**
  32. * This class contains several static convience methods utilized by Capsd while
  33. * doing data collection via jCIFS and the SMB (Server Message Block) protocol.
  34. *
  35. * @author <A HREF="mailto:mike@opennms.org">Mike </A>
  36. * @author <A HREF="http://www.opennms.org/">OpenNMS </A>
  37. */
  38. public abstract class SmbUtils {
  39. // NetBIOS Node Name Suffix Codes
  40. //
  41. /** Constant <code>WORKSTATION_SERVICE=0x00</code> */
  42. public static final int WORKSTATION_SERVICE = 0x00; // <computername>
  43. /** Constant <code>MESSENGER_SERVICE_A=0x01</code> */
  44. public static final int MESSENGER_SERVICE_A = 0x01; // <computername>
  45. /** Constant <code>MASTER_BROWSER_G=0x01</code> */
  46. public static final int MASTER_BROWSER_G = 0x01; // \\--__MSBROWSE__
  47. /** Constant <code>MESSENGER_SERVICE_B=0x03</code> */
  48. public static final int MESSENGER_SERVICE_B = 0x03; // <computername>
  49. /** Constant <code>RAS_SERVER_SERVICE=0x06</code> */
  50. public static final int RAS_SERVER_SERVICE = 0x06; // <computername>
  51. /** Constant <code>NETDDE_SERVICE=0x1F</code> */
  52. public static final int NETDDE_SERVICE = 0x1F; // <computername>
  53. /** Constant <code>FILE_SERVER_SERVICE=0x20</code> */
  54. public static final int FILE_SERVER_SERVICE = 0x20; // <computername>
  55. /** Constant <code>RAS_CLIENT_SERVICE=0x21</code> */
  56. public static final int RAS_CLIENT_SERVICE = 0x21; // <computername>
  57. /** Constant <code>MS_EXCHANGE_INTERCHANGE=0x22</code> */
  58. public static final int MS_EXCHANGE_INTERCHANGE = 0x22; // <computername>
  59. /** Constant <code>MS_EXCHANGE_STORE=0x23</code> */
  60. public static final int MS_EXCHANGE_STORE = 0x23; // <computername>
  61. /** Constant <code>MS_EXCHANGE_DIRECTORY=0x24</code> */
  62. public static final int MS_EXCHANGE_DIRECTORY = 0x24; // <computername>
  63. /** Constant <code>MODEM_SHARING_SERVER_SERVICE=0x30</code> */
  64. public static final int MODEM_SHARING_SERVER_SERVICE = 0x30; // <computername>
  65. /** Constant <code>MODEM_SHARING_CLIENT_SERVICE=0x31</code> */
  66. public static final int MODEM_SHARING_CLIENT_SERVICE = 0x31; // <computername>
  67. /** Constant <code>SMS_CLIENT_REMOTE_CONTROL=0x43</code> */
  68. public static final int SMS_CLIENT_REMOTE_CONTROL = 0x43; // <computername>
  69. /** Constant <code>SMS_ADMIN_REMOTE_CONTROL_TOOL=0x44</code> */
  70. public static final int SMS_ADMIN_REMOTE_CONTROL_TOOL = 0x44; // <computername>
  71. /** Constant <code>SMS_CLIENTS_REMOTE_CHAT=0x45</code> */
  72. public static final int SMS_CLIENTS_REMOTE_CHAT = 0x45; // <computername>
  73. /** Constant <code>SMS_CLIENTS_REMOTE_TRANSFER=0x46</code> */
  74. public static final int SMS_CLIENTS_REMOTE_TRANSFER = 0x46; // <computername>
  75. /** Constant <code>DEC_PATHWORKS_TCPIP_SERVICE_A=0x4C</code> */
  76. public static final int DEC_PATHWORKS_TCPIP_SERVICE_A = 0x4C; // <computername>
  77. /** Constant <code>DEC_PATHWORKS_TCPIP_SERVICE_B=0x52</code> */
  78. public static final int DEC_PATHWORKS_TCPIP_SERVICE_B = 0x52; // <computername>
  79. /** Constant <code>MS_EXCHANGE_MTA=0x87</code> */
  80. public static final int MS_EXCHANGE_MTA = 0x87; // <computername>
  81. /** Constant <code>MS_EXCHANGE_IMC=0x6A</code> */
  82. public static final int MS_EXCHANGE_IMC = 0x6A; // <computername>
  83. /** Constant <code>NETWORK_MONITOR_AGENT=0xBE</code> */
  84. public static final int NETWORK_MONITOR_AGENT = 0xBE; // <computername>
  85. /** Constant <code>NETWORK_MONITOR_APPLICATION=0xBF</code> */
  86. public static final int NETWORK_MONITOR_APPLICATION = 0xBF; // <computername>
  87. /** Constant <code>MESSENGER_SERVICE=0x03</code> */
  88. public static final int MESSENGER_SERVICE = 0x03; // <username>
  89. /** Constant <code>DOMAIN_NAME=0x00</code> */
  90. public static final int DOMAIN_NAME = 0x00; // <domain>
  91. /** Constant <code>DOMAIN_MASTER_BROWSER=0x1B</code> */
  92. public static final int DOMAIN_MASTER_BROWSER = 0x1B; // <domain>
  93. /** Constant <code>DOMAIN_CONTROLLERS=0x1C</code> */
  94. public static final int DOMAIN_CONTROLLERS = 0x1C; // <domain>
  95. /** Constant <code>MASTER_BROWSER_U=0x1D</code> */
  96. public static final int MASTER_BROWSER_U = 0x1D; // <domain>
  97. /** Constant <code>BROWSER_SERVICE_ELECTIONS=0x1E</code> */
  98. public static final int BROWSER_SERVICE_ELECTIONS = 0x1E; // <domain>
  99. /** Constant <code>INTERNET_INFORMATION_SERVER_G=0x1C</code> */
  100. public static final int INTERNET_INFORMATION_SERVER_G = 0x1C; // INET~SERVICES
  101. // (GROUP)
  102. /** Constant <code>INTERNET_INFORMATION_SERVER_U=0x00</code> */
  103. public static final int INTERNET_INFORMATION_SERVER_U = 0x00; // IS~<computername>
  104. // (UNIQUE)
  105. /** Constant <code>LOTUS_NOTES_SERVER_SERVICE=0x2B</code> */
  106. public static final int LOTUS_NOTES_SERVER_SERVICE = 0x2B; // <computername>
  107. /** Constant <code>LOTUS_NOTES_IRIS_MULTICAST=0x2F</code> */
  108. public static final int LOTUS_NOTES_IRIS_MULTICAST = 0x2F; // IRISMULTICAST
  109. /** Constant <code>LOTUS_NOTES_IRIS_NAME_SERVER=0x33</code> */
  110. public static final int LOTUS_NOTES_IRIS_NAME_SERVER = 0x33; // IRISNAMESERVER
  111. /** Constant <code>DCA_IRMALAN_GATEWAY_SERVER_SERVICE=0x20</code> */
  112. public static final int DCA_IRMALAN_GATEWAY_SERVER_SERVICE = 0x20; // FORTE_$ND800ZA
  113. /**
  114. * This method attempts to determine the authentication domain for a remote
  115. * host. The list of NbtAddress objects is processed in order to find an
  116. * entry with a DOMAIN_NAME (0x00) suffix. WORKSTATION_SERVICE and
  117. * INTERNET_INFORMATION_SERVER share the same 0x00 suffix so these entries
  118. * must be ignored while processing the address list.
  119. *
  120. * @param addresses
  121. * List of NbtAddress objects associated with the remote host.
  122. * @param cname
  123. * NetBIOS name of the remote host.
  124. *
  125. * @return remote host's authentication domain or null if unavailable.
  126. */
  127. static String getAuthenticationDomainName(NbtAddress[] addresses, String cname) {
  128. String domain = null;
  129. if (addresses != null) {
  130. for (int i = 0; i < addresses.length && domain == null; i++) {
  131. NbtAddress addr = addresses[i];
  132. // look at the domain name type and search for
  133. // anything that returns 0x00. Then check other
  134. // criteria to eliminate IIS and other nondesired
  135. // elements from the list
  136. //
  137. if (addr.getNameType() == DOMAIN_NAME) {
  138. // the 0x00 suffic can be one of the following types:
  139. //
  140. // WORKSTATION_SERVICE
  141. // INTERNET_INFORMATION_SERVER
  142. // DOMAIN_NAME
  143. //
  144. // Eliminate the Workstation service by verifying that
  145. // the NetBIOS name does not equal the current
  146. // computer name.
  147. //
  148. // Eliminate the IIS servers hits by checking for the
  149. // string prefix IS~ in the NetBIOS name.
  150. //
  151. if (!addr.getHostName().equals(cname) && !addr.getHostName().startsWith("IS~")) {
  152. domain = addr.getHostName();
  153. }
  154. }
  155. }
  156. }
  157. return domain;
  158. }
  159. /**
  160. * Returns the operating system label to be associated with a node in 'node'
  161. * table in the databse. This call should be made after an attempt to
  162. * determine if the interface supports Microsoft Exchange. This is
  163. * determined by the {@link MSExchangePlugin MSExchangePlugin}class.
  164. *
  165. * @param nativeOS
  166. * OS string returned by jCIFS following SMB session
  167. * establishment with the remote host.
  168. * @param addresses
  169. * array of NbtAddress objects associated with the remote host
  170. * being tested.
  171. * @param isSamba
  172. * <em>true</em> if it has been derived that the remote system
  173. * is running Samba.
  174. * @param hasExchange
  175. * <em>true</em> if the service supports microsoft exhange.
  176. *
  177. * @return The Operating system label
  178. */
  179. static String getOsLabel(String nativeOS, NbtAddress[] addresses, boolean isSamba, boolean hasExchange) {
  180. String osLabel = null;
  181. // Given the operating system value returned by
  182. // jCIFS via SMB (nativeOS) now see if we can derive
  183. // anything else from the SMB data we've collected
  184. // in order to be more precise with our OS label.
  185. //
  186. if (nativeOS == null) {
  187. // HACK: nativeOS will be null if the share enumeration
  188. // failed for the remote host. If however the box is
  189. // running Samba we can safely assume the OS is either
  190. // Linux or UNIX.
  191. //
  192. if (isSamba) {
  193. osLabel = "Linux/UNIX";
  194. } else if (hasExchange) {
  195. osLabel = "Windows Server"; // Don't know if we have Win 2000 or
  196. // NT 4.0
  197. }
  198. } else if (nativeOS.length() == 0) {
  199. // HACK: Windows 95/98 boxes don't appear to give us the operating
  200. // system so if we have successfully enumerate the shares on a
  201. // server
  202. // but the operating system return is an emtpy string then we will
  203. // assume
  204. // it is a Win 95/98 box.
  205. //
  206. osLabel = "Windows 95/98";
  207. } else if (nativeOS.equalsIgnoreCase("Unix")) {
  208. // jCIFS reports "Unix" but the remote OS may actually
  209. // be Linux.
  210. //
  211. osLabel = "Linux/UNIX";
  212. } else if (nativeOS.equalsIgnoreCase("Windows 5.0")) {
  213. // jCIFS reports "Windows 5.0"...switch this to "Windows 2000"
  214. //
  215. nativeOS = "Windows 2000";
  216. if (hasExchange || isNTServer(addresses))
  217. osLabel = nativeOS.concat(" Server");
  218. else
  219. osLabel = nativeOS;
  220. } else if (nativeOS.startsWith("Windows NT")) {
  221. // Windows NT
  222. //
  223. if (hasExchange || isNTServer(addresses))
  224. osLabel = nativeOS.concat(" Server");
  225. else
  226. osLabel = nativeOS;
  227. } else {
  228. osLabel = nativeOS;
  229. }
  230. return osLabel;
  231. }
  232. /**
  233. * This method is responsible for taking an array of jCIFS NbtAddress
  234. * objects associated with a particular node and determining if that node is
  235. * an NT server versus an NT workstation based on the services it has
  236. * registered.
  237. *
  238. * If the remote host is registered as a DOMAIN_CONTROLLERS or a
  239. * MS_EXCHANGE_MTA we return 'true'; otherwise, 'false' is returned.
  240. *
  241. * @param addresses
  242. * Array of NbtAddress objects associated with the remote host
  243. * being tested.
  244. *
  245. * @return <em>true</em> if NT Server, <em>false</em> otherwise.
  246. */
  247. static boolean isNTServer(NbtAddress[] addresses) {
  248. boolean isNTServer = false;
  249. if (addresses != null) {
  250. for (int i = 0; i < addresses.length; i++) {
  251. NbtAddress nbtAddr = addresses[i];
  252. // Domain Controller
  253. //
  254. // If we find an address with a name sufix equal to
  255. // 0x1C (DOMAIN_CONTROLLER or INTERNET_INFORMATION_SERVER_G)
  256. // and the hostnamethis box is an NT server.
  257. //
  258. if (nbtAddr.getNameType() == DOMAIN_CONTROLLERS && !nbtAddr.getHostName().equals("INET~SERVICES")) {
  259. isNTServer = true;
  260. break;
  261. }
  262. // Exchange Server
  263. //
  264. // Looks like a Windows 2000 box running full-blown
  265. // MS Outlook will appear in the NetBIOS registry
  266. // as MS_EXCHANGE_IMC and MS_EXCHANGE_MTA so we
  267. // can't use those suffixes to identify a server
  268. //
  269. // So...if we detect MICROSOFT_EXCHANGE_STORE or
  270. // MICROSOFT_EXCHANGE_DIRECTORY we will assume we
  271. // have a server.
  272. //
  273. // ??? Can we really assume we have an exchange server
  274. // based on this and therefore NT Server???
  275. //
  276. if (nbtAddr.getNameType() == MS_EXCHANGE_STORE || nbtAddr.getNameType() == MS_EXCHANGE_DIRECTORY) {
  277. isNTServer = true;
  278. break;
  279. }
  280. // Internet Information Server
  281. //
  282. // If we find an address with a name suffix equal to
  283. // 0x1C (INTERNET_INFORMATION_SERVER_G) we are done, we've
  284. // found an NT server.
  285. //
  286. // If we find an address with a name suffix equal to
  287. // 0x00 (INTERNET_INFORMATION_SERVER_U) we must go on to
  288. // verify that its NetBIOS name begins with "IS~". If so
  289. // we've found an NT server.
  290. //
  291. // WARNING: Unfortunately IIS on an NT Server looks no different
  292. // than Personal Web Server on a Windows 2000 Professional
  293. // box from the standpoint of the NetBIOS name suffixes.
  294. // So we can't use it to distinguish between server and
  295. // workstation.
  296. /*
  297. * if (nbtAddr.getNameType() == INTERNET_INFORMATION_SERVER_G) {
  298. * isNTServer = true; break; } else if (nbtAddr.getNameType() ==
  299. * INTERNET_INFORMATION_SERVER_U) { if
  300. * (nbtAddr.getHostName().startsWith("IS~")) { isNTServer =
  301. * true; break; } }
  302. */
  303. }
  304. }
  305. return isNTServer;
  306. }
  307. /**
  308. * Convenience method which takes an SmbAuth object with userid and password
  309. * information and the NetBIOS name for a remote server and builds the
  310. * appropriate SMB url string which can be used to enumerate the server's
  311. * shares.
  312. *
  313. * @param smbAuth
  314. * SMB Authentication object w/ userid/password info
  315. * @param cname
  316. * NetBIOS address of remote server
  317. *
  318. * @return URL string which can be used in a subsequent SmbFile() call.
  319. */
  320. static String getSmbURL(SmbAuth smbAuth, String cname) {
  321. // Build SMB server url
  322. //
  323. // If we don't have a valid SmbAuth object url has format:
  324. // smb://<netbios_name>
  325. //
  326. // For server authentication url has format:
  327. // smb://<userid>:<password>@<netbios_name>
  328. //
  329. // For domain authentication url has format:
  330. // smb://<domain>;<userid>:<password>@<netbios_name>
  331. // Set url parameters
  332. String domainParm = null;
  333. String useridParm = null;
  334. String passwordParm = null;
  335. if (smbAuth != null) {
  336. useridParm = smbAuth.getUser();
  337. passwordParm = smbAuth.getPassword();
  338. if (smbAuth.getType().equalsIgnoreCase("domain"))
  339. domainParm = smbAuth.getContent();
  340. }
  341. // Build url from parms
  342. String url = "smb://";
  343. if (domainParm != null)
  344. url = url.concat(domainParm + ";");
  345. if (useridParm != null)
  346. url = url.concat(useridParm);
  347. if (passwordParm != null)
  348. url = url.concat(":" + passwordParm);
  349. if (useridParm != null)
  350. url = url.concat("@");
  351. url = url.concat(cname);
  352. return url;
  353. }
  354. }