private static ClassifyResult ClassifyUncLink(Uri uri, WindowsIdentity authenticatedUser) { WindowsImpersonationContext windowsImpersonationContext = null; ClassifyResult classifyResult = null; ClassifyResult result; try { windowsImpersonationContext = Utils.ImpersonateUser(authenticatedUser); UriFlags uriFlags = UriFlags.Other; if (!Utils.IsValidUncUri(uri)) { result = new ClassifyResult(uri, ClassificationError.InvalidUri); } else { if (uri.Segments.Length == 1) { try { UncObjectId objectId = new UncObjectId(uri, UriFlags.Unc); UncSession uncSession = UncSession.Open(objectId, new WindowsPrincipal(WindowsIdentity.GetCurrent())); uncSession.GetView(null, null, new PropertyDefinition[0]); } catch (AccessDeniedException) { return(new ClassifyResult(uri, ClassificationError.AccessDenied)); } catch (ObjectMovedOrDeletedException) { return(new ClassifyResult(uri, ClassificationError.ConnectionFailed)); } uriFlags = UriFlags.Unc; } else if (uri.Segments.Length >= 2) { if (uri.Segments.Length >= 3) { try { FileSystemInfo fileSystemInfo = new FileInfo(uri.LocalPath); if (fileSystemInfo.Attributes == (FileAttributes)(-1)) { return(new ClassifyResult(uri, ClassificationError.ObjectNotFound)); } if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { uriFlags = UriFlags.UncFolder; } else { uriFlags = UriFlags.UncDocument; } } catch (UnauthorizedAccessException) { classifyResult = new ClassifyResult(uri, ClassificationError.AccessDenied); } catch (FileNotFoundException) { classifyResult = new ClassifyResult(uri, ClassificationError.ObjectNotFound); } catch (IOException) { classifyResult = new ClassifyResult(uri, ClassificationError.AccessDenied); } } if (classifyResult != null || uri.Segments.Length == 2) { IntPtr intPtr; int num = UncSession.NetShareGetInfo(uri.Host, Uri.UnescapeDataString(uri.Segments[1].TrimEnd(new char[] { '\\', '/' })), 1, out intPtr); if (intPtr != IntPtr.Zero) { UncSession.NetApiBufferFree(intPtr); } else if (num == 5 || num == 2311) { classifyResult = new ClassifyResult(uri, ClassificationError.AccessDenied); } else if (num == 53 || num == 2250) { classifyResult = new ClassifyResult(uri, ClassificationError.ConnectionFailed); } else if (num == 2310) { classifyResult = new ClassifyResult(uri, ClassificationError.ObjectNotFound); } else if (num != 0) { classifyResult = new ClassifyResult(uri, ClassificationError.UnknownError); } uriFlags = UriFlags.UncDocumentLibrary; } if (classifyResult != null) { return(classifyResult); } } if ((uriFlags & UriFlags.Unc) != (UriFlags)0) { result = new ClassifyResult(new UncObjectId(uri, uriFlags), uri, uriFlags); } else { result = new ClassifyResult(uri, ClassificationError.ObjectNotFound); } } } catch { Utils.UndoContext(ref windowsImpersonationContext); throw; } finally { Utils.UndoContext(ref windowsImpersonationContext); } return(result); }
public static UncDocumentLibrary Read(UncSession session, ObjectId objectId) { if (session == null) { throw new ArgumentNullException("session"); } if (objectId == null) { throw new ArgumentNullException("objectId"); } UncObjectId uncObjectId = objectId as UncObjectId; if (uncObjectId == null || uncObjectId.UriFlags != UriFlags.UncDocumentLibrary) { throw new ArgumentException("objectId"); } if (!session.Uri.IsBaseOf(uncObjectId.Path)) { throw new ArgumentException("objectId"); } if (uncObjectId.Path.Segments.Length != 2) { throw new ArgumentException("uncId"); } return(Utils.DoUncTask <UncDocumentLibrary>(session.Identity, uncObjectId, false, Utils.MethodType.Read, delegate { string netName = Uri.UnescapeDataString(uncObjectId.Path.Segments[1].TrimEnd(new char[] { '\\', '/' })); IntPtr zero = IntPtr.Zero; int num = UncSession.NetShareGetInfo(uncObjectId.Path.Host, netName, 1, out zero); try { if (num == 0) { UncSession.SHARE_INFO_1 share_INFO_ = (UncSession.SHARE_INFO_1)Marshal.PtrToStructure(zero, typeof(UncSession.SHARE_INFO_1)); try { return new UncDocumentLibrary(session, uncObjectId, share_INFO_.Remark); } catch (IOException innerException) { throw new AccessDeniedException(uncObjectId, Strings.ExAccessDenied(uncObjectId.Path.LocalPath), innerException); } } if (num == 5 || num == 2311) { throw new AccessDeniedException(uncObjectId, Strings.ExAccessDenied(uncObjectId.Path.LocalPath)); } if (num == 53 || num == 2250) { throw new ConnectionException(uncObjectId, Strings.ExCannotConnectToMachine(uncObjectId.Path.Host)); } throw new ObjectNotFoundException(uncObjectId, Strings.ExObjectNotFound(uncObjectId.Path.LocalPath)); } finally { if (zero != IntPtr.Zero) { UncSession.NetApiBufferFree(zero); } } UncDocumentLibrary result; return result; })); }