Dfs.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System.IO;
  18. using SharpCifs.Util;
  19. using SharpCifs.Util.Sharpen;
  20. namespace SharpCifs.Smb
  21. {
  22. public class Dfs
  23. {
  24. internal class CacheEntry
  25. {
  26. internal long Expiration;
  27. internal Hashtable Map;
  28. internal CacheEntry(long ttl)
  29. {
  30. if (ttl == 0)
  31. {
  32. ttl = Ttl;
  33. }
  34. Expiration = Runtime.CurrentTimeMillis() + ttl * 1000L;
  35. Map = new Hashtable();
  36. }
  37. }
  38. internal static LogStream Log = LogStream.GetInstance();
  39. internal static readonly bool StrictView
  40. = Config.GetBoolean("jcifs.smb.client.dfs.strictView", false);
  41. internal static readonly long Ttl
  42. = Config.GetLong("jcifs.smb.client.dfs.ttl", 300);
  43. internal static readonly bool Disabled
  44. = Config.GetBoolean("jcifs.smb.client.dfs.disabled", false);
  45. internal static CacheEntry FalseEntry = new CacheEntry(0L);
  46. internal CacheEntry Domains;
  47. internal CacheEntry Referrals;
  48. /// <exception cref="SharpCifs.Smb.SmbAuthException"></exception>
  49. public virtual Hashtable GetTrustedDomains(NtlmPasswordAuthentication auth)
  50. {
  51. if (Disabled || auth.Domain == "?")
  52. {
  53. return null;
  54. }
  55. if (Domains != null && Runtime.CurrentTimeMillis() > Domains.Expiration)
  56. {
  57. Domains = null;
  58. }
  59. if (Domains != null)
  60. {
  61. return Domains.Map;
  62. }
  63. try
  64. {
  65. UniAddress addr = UniAddress.GetByName(auth.Domain, true);
  66. SmbTransport trans = SmbTransport.GetSmbTransport(addr, 0);
  67. CacheEntry entry = new CacheEntry(Ttl * 10L);
  68. DfsReferral dr = trans.GetDfsReferrals(auth, string.Empty, 0);
  69. if (dr != null)
  70. {
  71. DfsReferral start = dr;
  72. do
  73. {
  74. string domain = dr.Server.ToLower();
  75. entry.Map.Put(domain, new Hashtable());
  76. dr = dr.Next;
  77. }
  78. while (dr != start);
  79. Domains = entry;
  80. return Domains.Map;
  81. }
  82. }
  83. catch (IOException ioe)
  84. {
  85. if (Log.Level >= 3)
  86. {
  87. Runtime.PrintStackTrace(ioe, Log);
  88. }
  89. if (StrictView && ioe is SmbAuthException)
  90. {
  91. throw (SmbAuthException)ioe;
  92. }
  93. }
  94. return null;
  95. }
  96. /// <exception cref="SharpCifs.Smb.SmbAuthException"></exception>
  97. public virtual bool IsTrustedDomain(string domain, NtlmPasswordAuthentication auth)
  98. {
  99. Hashtable domains = GetTrustedDomains(auth);
  100. if (domains == null)
  101. {
  102. return false;
  103. }
  104. domain = domain.ToLower();
  105. return domains.Get(domain) != null;
  106. }
  107. /// <exception cref="SharpCifs.Smb.SmbAuthException"></exception>
  108. public virtual SmbTransport GetDc(string domain, NtlmPasswordAuthentication auth)
  109. {
  110. if (Disabled)
  111. {
  112. return null;
  113. }
  114. try
  115. {
  116. UniAddress addr = UniAddress.GetByName(domain, true);
  117. SmbTransport trans = SmbTransport.GetSmbTransport(addr, 0);
  118. DfsReferral dr = trans.GetDfsReferrals(auth, "\\" + domain, 1);
  119. if (dr != null)
  120. {
  121. DfsReferral start = dr;
  122. IOException e = null;
  123. do
  124. {
  125. try
  126. {
  127. addr = UniAddress.GetByName(dr.Server);
  128. return SmbTransport.GetSmbTransport(addr, 0);
  129. }
  130. catch (IOException ioe)
  131. {
  132. e = ioe;
  133. }
  134. dr = dr.Next;
  135. }
  136. while (dr != start);
  137. throw e;
  138. }
  139. }
  140. catch (IOException ioe)
  141. {
  142. if (Log.Level >= 3)
  143. {
  144. Runtime.PrintStackTrace(ioe, Log);
  145. }
  146. if (StrictView && ioe is SmbAuthException)
  147. {
  148. throw (SmbAuthException)ioe;
  149. }
  150. }
  151. return null;
  152. }
  153. /// <exception cref="SharpCifs.Smb.SmbAuthException"></exception>
  154. public virtual DfsReferral GetReferral(SmbTransport trans,
  155. string domain,
  156. string root,
  157. string path,
  158. NtlmPasswordAuthentication auth)
  159. {
  160. if (Disabled)
  161. {
  162. return null;
  163. }
  164. try
  165. {
  166. string p = "\\" + domain + "\\" + root;
  167. if (path != null)
  168. {
  169. p += path;
  170. }
  171. DfsReferral dr = trans.GetDfsReferrals(auth, p, 0);
  172. if (dr != null)
  173. {
  174. return dr;
  175. }
  176. }
  177. catch (IOException ioe)
  178. {
  179. if (Log.Level >= 4)
  180. {
  181. Runtime.PrintStackTrace(ioe, Log);
  182. }
  183. if (StrictView && ioe is SmbAuthException)
  184. {
  185. throw (SmbAuthException)ioe;
  186. }
  187. }
  188. return null;
  189. }
  190. /// <exception cref="SharpCifs.Smb.SmbAuthException"></exception>
  191. public virtual DfsReferral Resolve(string domain,
  192. string root,
  193. string path,
  194. NtlmPasswordAuthentication auth)
  195. {
  196. lock (this)
  197. {
  198. DfsReferral dr = null;
  199. long now = Runtime.CurrentTimeMillis();
  200. if (Disabled || root.Equals("IPC$"))
  201. {
  202. return null;
  203. }
  204. Hashtable domains = GetTrustedDomains(auth);
  205. if (domains != null)
  206. {
  207. domain = domain.ToLower();
  208. Hashtable roots = (Hashtable)domains.Get(domain);
  209. if (roots != null)
  210. {
  211. SmbTransport trans = null;
  212. root = root.ToLower();
  213. CacheEntry links = (CacheEntry)roots.Get(root);
  214. if (links != null && now > links.Expiration)
  215. {
  216. //Sharpen.Collections.Remove(roots, root);
  217. roots.Remove(root);
  218. links = null;
  219. }
  220. if (links == null)
  221. {
  222. if ((trans = GetDc(domain, auth)) == null)
  223. {
  224. return null;
  225. }
  226. dr = GetReferral(trans, domain, root, path, auth);
  227. if (dr != null)
  228. {
  229. int len = 1 + domain.Length + 1 + root.Length;
  230. links = new CacheEntry(0L);
  231. DfsReferral tmp = dr;
  232. do
  233. {
  234. if (path == null)
  235. {
  236. // TODO: fix this
  237. //tmp.map = links.map;
  238. tmp.Key = "\\";
  239. }
  240. tmp.PathConsumed -= len;
  241. tmp = tmp.Next;
  242. }
  243. while (tmp != dr);
  244. if (dr.Key != null)
  245. {
  246. links.Map.Put(dr.Key, dr);
  247. }
  248. roots.Put(root, links);
  249. }
  250. else
  251. {
  252. if (path == null)
  253. {
  254. roots.Put(root, FalseEntry);
  255. }
  256. }
  257. }
  258. else
  259. {
  260. if (links == FalseEntry)
  261. {
  262. links = null;
  263. }
  264. }
  265. if (links != null)
  266. {
  267. string link = "\\";
  268. dr = (DfsReferral)links.Map.Get(link);
  269. if (dr != null && now > dr.Expiration)
  270. {
  271. //Sharpen.Collections.Remove(links.map, link);
  272. links.Map.Remove(link);
  273. dr = null;
  274. }
  275. if (dr == null)
  276. {
  277. if (trans == null)
  278. {
  279. if ((trans = GetDc(domain, auth)) == null)
  280. {
  281. return null;
  282. }
  283. }
  284. dr = GetReferral(trans, domain, root, path, auth);
  285. if (dr != null)
  286. {
  287. dr.PathConsumed -= 1 + domain.Length + 1 + root.Length;
  288. dr.Link = link;
  289. links.Map.Put(link, dr);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. if (dr == null && path != null)
  296. {
  297. if (Referrals != null && now > Referrals.Expiration)
  298. {
  299. Referrals = null;
  300. }
  301. if (Referrals == null)
  302. {
  303. Referrals = new CacheEntry(0);
  304. }
  305. string key = "\\" + domain + "\\" + root;
  306. if (path.Equals("\\") == false)
  307. {
  308. key += path;
  309. }
  310. key = key.ToLower();
  311. //ListIterator<object> iter = new ListIterator<object>(referrals.map.Keys.GetEnumerator(), 0);
  312. foreach (var current in Referrals.Map.Keys)
  313. {
  314. string _key = (string)current;
  315. int klen = _key.Length;
  316. bool match = false;
  317. if (klen == key.Length)
  318. {
  319. match = _key.Equals(key);
  320. }
  321. else
  322. {
  323. if (klen < key.Length)
  324. {
  325. match = _key.RegionMatches(false, 0, key, 0, klen) && key[klen] == '\\';
  326. }
  327. }
  328. if (match)
  329. {
  330. dr = (DfsReferral)Referrals.Map.Get(_key);
  331. }
  332. }
  333. }
  334. return dr;
  335. }
  336. }
  337. internal virtual void Insert(string path, DfsReferral dr)
  338. {
  339. lock (this)
  340. {
  341. int s1;
  342. int s2;
  343. string server;
  344. string share;
  345. string key;
  346. if (Disabled)
  347. {
  348. return;
  349. }
  350. s1 = path.IndexOf('\\', 1);
  351. s2 = path.IndexOf('\\', s1 + 1);
  352. server = Runtime.Substring(path, 1, s1);
  353. share = Runtime.Substring(path, s1 + 1, s2);
  354. key = Runtime.Substring(path, 0, dr.PathConsumed).ToLower();
  355. int ki = key.Length;
  356. while (ki > 1 && key[ki - 1] == '\\')
  357. {
  358. ki--;
  359. }
  360. if (ki < key.Length)
  361. {
  362. key = Runtime.Substring(key, 0, ki);
  363. }
  364. dr.PathConsumed -= 1 + server.Length + 1 + share.Length;
  365. if (Referrals != null && (Runtime.CurrentTimeMillis() + 10000) > Referrals.Expiration)
  366. {
  367. Referrals = null;
  368. }
  369. if (Referrals == null)
  370. {
  371. Referrals = new CacheEntry(0);
  372. }
  373. Referrals.Map.Put(key, dr);
  374. }
  375. }
  376. }
  377. }