Code refactoring: Added NTFileSystemHelper.ToNTStatus method to translate IFileSystem exceptions to NTStatus

This commit is contained in:
Tal Aloni 2017-02-09 14:28:37 +02:00
parent fd26380dab
commit a9f92df47b
3 changed files with 99 additions and 226 deletions

View file

@ -62,9 +62,10 @@ namespace SMBLibrary.Server
{ {
entries = fileSystem.ListEntriesInDirectory(path); entries = fileSystem.ListEntriesInDirectory(path);
} }
catch (UnauthorizedAccessException) catch (Exception ex)
{ {
return NTStatus.STATUS_ACCESS_DENIED; NTStatus status = ToNTStatus(ex);
return status; ;
} }
entries = GetFiltered(entries, expression); entries = GetFiltered(entries, expression);

View file

@ -25,34 +25,22 @@ namespace SMBLibrary.Server
{ {
fileSystem.SetAttributes(openFile.Path, isHidden, isReadonly, isArchived); fileSystem.SetAttributes(openFile.Path, isHidden, isReadonly, isArchived);
} }
catch (UnauthorizedAccessException) catch (Exception ex)
{ {
state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file attributes on '{0}'. Access Denied.", openFile.Path); NTStatus status = ToNTStatus(ex);
return NTStatus.STATUS_ACCESS_DENIED; state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file attributes on '{0}'. {1}.", openFile.Path, status);
return status;
} }
try try
{ {
fileSystem.SetDates(openFile.Path, basicInformation.CreationTime, basicInformation.LastWriteTime, basicInformation.LastAccessTime); fileSystem.SetDates(openFile.Path, basicInformation.CreationTime, basicInformation.LastWriteTime, basicInformation.LastAccessTime);
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. {1}.", openFile.Path, status);
{ return status;
state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. Sharing Violation.", openFile.Path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. Data Error.", openFile.Path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. Access Denied.", openFile.Path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
return NTStatus.STATUS_SUCCESS; return NTStatus.STATUS_SUCCESS;
} }
@ -78,29 +66,11 @@ namespace SMBLibrary.Server
} }
fileSystem.Move(openFile.Path, destination); fileSystem.Move(openFile.Path, destination);
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. {1}.", openFile.Path, status);
{ return status;
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Sharing Violation.", openFile.Path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
if (errorCode == (ushort)Win32Error.ERROR_ALREADY_EXISTS)
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Already Exists.", openFile.Path);
return NTStatus.STATUS_OBJECT_NAME_EXISTS;
}
else
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Data Error.", openFile.Path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. Access Denied.", openFile.Path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
openFile.Path = destination; openFile.Path = destination;
return NTStatus.STATUS_SUCCESS; return NTStatus.STATUS_SUCCESS;
@ -120,24 +90,11 @@ namespace SMBLibrary.Server
state.LogToServer(Severity.Information, "SetFileInformation: Deleting file '{0}'", openFile.Path); state.LogToServer(Severity.Information, "SetFileInformation: Deleting file '{0}'", openFile.Path);
fileSystem.Delete(openFile.Path); fileSystem.Delete(openFile.Path);
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "SetFileInformation: Error deleting '{0}'. {1}.", openFile.Path, status);
{ return status;
state.LogToServer(Severity.Information, "SetFileInformation: Error deleting '{0}'. Sharing Violation.", openFile.Path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Information, "SetFileInformation: Error deleting '{0}'. Data Error.", openFile.Path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Information, "SetFileInformation: Error deleting '{0}', Access Denied.", openFile.Path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
} }
return NTStatus.STATUS_SUCCESS; return NTStatus.STATUS_SUCCESS;
@ -149,24 +106,11 @@ namespace SMBLibrary.Server
{ {
openFile.Stream.SetLength((long)allocationSize); openFile.Stream.SetLength((long)allocationSize);
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. {1}.", openFile.Path, status);
{ return status;
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. Sharing Violation.", openFile.Path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. Data Error.", openFile.Path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. Access Denied.", openFile.Path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
return NTStatus.STATUS_SUCCESS; return NTStatus.STATUS_SUCCESS;
} }
@ -177,24 +121,11 @@ namespace SMBLibrary.Server
{ {
openFile.Stream.SetLength((long)endOfFile); openFile.Stream.SetLength((long)endOfFile);
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. {1}.", openFile.Path, status);
{ return status;
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. Sharing Violation.", openFile.Path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. Data Error.", openFile.Path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. Access Denied.", openFile.Path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
return NTStatus.STATUS_SUCCESS; return NTStatus.STATUS_SUCCESS;
} }

View file

@ -48,11 +48,12 @@ namespace SMBLibrary.Server
{ {
entry = fileSystem.GetEntry(path); entry = fileSystem.GetEntry(path);
} }
catch (ArgumentException) catch (Exception ex)
{ {
state.LogToServer(Severity.Debug, "CreateFile: Error retrieving '{0}'. Argument Exception.", path); NTStatus status = ToNTStatus(ex);
state.LogToServer(Severity.Debug, "CreateFile: Error retrieving '{0}'. {1}.", path, status);
entry = null; entry = null;
return NTStatus.STATUS_OBJECT_PATH_SYNTAX_BAD; return status;
} }
if (createDisposition == CreateDisposition.FILE_OPEN) if (createDisposition == CreateDisposition.FILE_OPEN)
@ -100,24 +101,11 @@ namespace SMBLibrary.Server
entry = fileSystem.CreateFile(path); entry = fileSystem.CreateFile(path);
} }
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "CreateFile: Error creating '{0}'. {1}.", path, status);
{ return status;
state.LogToServer(Severity.Debug, "CreateFile: Error creating '{0}'. Sharing violation.", path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "CreateFile: Error creating '{0}'. Data Error.", path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "CreateFile: Error creating '{0}'. Access Denied.", path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
} }
else if (createDisposition == CreateDisposition.FILE_OPEN_IF || else if (createDisposition == CreateDisposition.FILE_OPEN_IF ||
@ -150,21 +138,11 @@ namespace SMBLibrary.Server
entry = fileSystem.CreateFile(path); entry = fileSystem.CreateFile(path);
} }
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "CreateFile: Error creating '{0}'. {1}.", path, status);
{ return status;
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
return NTStatus.STATUS_ACCESS_DENIED;
} }
} }
else else
@ -184,21 +162,11 @@ namespace SMBLibrary.Server
Stream temp = fileSystem.OpenFile(path, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite); Stream temp = fileSystem.OpenFile(path, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);
temp.Close(); temp.Close();
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "CreateFile: Error truncating '{0}'. {1}.", path, status);
{ return status;
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
return NTStatus.STATUS_ACCESS_DENIED;
} }
} }
} }
@ -220,39 +188,11 @@ namespace SMBLibrary.Server
{ {
stream = fileSystem.OpenFile(path, FileMode.Open, fileAccess, fileShare); stream = fileSystem.OpenFile(path, FileMode.Open, fileAccess, fileShare);
} }
catch (ArgumentException) catch (Exception ex)
{ {
state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. Argument Exception.", path); NTStatus status = ToNTStatus(ex);
return NTStatus.STATUS_OBJECT_PATH_SYNTAX_BAD; state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. {1}.", path, status);
} return status;
catch (DirectoryNotFoundException)
{
state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. Directory not found.", path);
return NTStatus.STATUS_NO_SUCH_FILE;
}
catch (FileNotFoundException)
{
state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. File not found.", path);
return NTStatus.STATUS_NO_SUCH_FILE;
}
catch (IOException ex)
{
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
{
state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. Sharing violation.", path);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. Data Error.", path);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "OpenFile: Cannot open '{0}'. Access Denied.", path);
return NTStatus.STATUS_ACCESS_DENIED;
} }
if (buffered) if (buffered)
@ -294,30 +234,11 @@ namespace SMBLibrary.Server
data = new byte[maxCount]; data = new byte[maxCount];
bytesRead = stream.Read(data, 0, maxCount); bytesRead = stream.Read(data, 0, maxCount);
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION) state.LogToServer(Severity.Debug, "ReadFile: Cannot read '{0}'. {1}.", openFilePath, status);
{ return status;
// Returning STATUS_SHARING_VIOLATION is undocumented but apparently valid
state.LogToServer(Severity.Debug, "ReadFile: Cannot read '{0}'. Sharing Violation.", openFilePath);
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "ReadFile: Cannot read '{0}'. Data Error.", openFilePath);
return NTStatus.STATUS_DATA_ERROR;
}
}
catch (ArgumentOutOfRangeException)
{
state.LogToServer(Severity.Debug, "ReadFile: Cannot read '{0}'. Offset Out Of Range.", openFilePath);
return NTStatus.STATUS_DATA_ERROR;
}
catch (UnauthorizedAccessException)
{
state.LogToServer(Severity.Debug, "ReadFile: Cannot read '{0}', Access Denied.", openFilePath);
return NTStatus.STATUS_ACCESS_DENIED;
} }
if (bytesRead < maxCount) if (bytesRead < maxCount)
@ -352,40 +273,60 @@ namespace SMBLibrary.Server
{ {
stream.Seek(offset, SeekOrigin.Begin); stream.Seek(offset, SeekOrigin.Begin);
stream.Write(data, 0, data.Length); stream.Write(data, 0, data.Length);
numberOfBytesWritten = data.Length;
return NTStatus.STATUS_SUCCESS;
} }
catch (IOException ex) catch (Exception ex)
{ {
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex); NTStatus status = ToNTStatus(ex);
if (errorCode == (ushort)Win32Error.ERROR_DISK_FULL) state.LogToServer(Severity.Debug, "WriteFile: Cannot write '{0}'. {1}.", openFilePath, status);
{ return status;
state.LogToServer(Severity.Debug, "WriteFile: Cannot write '{0}'. Disk Full.", openFilePath);
return NTStatus.STATUS_DISK_FULL;
}
else if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
{
state.LogToServer(Severity.Debug, "WriteFile: Cannot write '{0}'. Sharing Violation.", openFilePath);
// Returning STATUS_SHARING_VIOLATION is undocumented but apparently valid
return NTStatus.STATUS_SHARING_VIOLATION;
}
else
{
state.LogToServer(Severity.Debug, "WriteFile: Cannot write '{0}'. Data Error.", openFilePath);
return NTStatus.STATUS_DATA_ERROR;
}
} }
catch (ArgumentOutOfRangeException) numberOfBytesWritten = data.Length;
return NTStatus.STATUS_SUCCESS;
}
}
/// <param name="exception">IFileSystem exception</param>
private static NTStatus ToNTStatus(Exception exception)
{
if (exception is ArgumentException)
{
return NTStatus.STATUS_OBJECT_PATH_SYNTAX_BAD;
}
else if (exception is DirectoryNotFoundException)
{
return NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
}
else if (exception is FileNotFoundException)
{
return NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
}
else if (exception is IOException)
{
ushort errorCode = IOExceptionHelper.GetWin32ErrorCode((IOException)exception);
if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
{
return NTStatus.STATUS_SHARING_VIOLATION;
}
else if (errorCode == (ushort)Win32Error.ERROR_DISK_FULL)
{
return NTStatus.STATUS_DISK_FULL;
}
else if (errorCode == (ushort)Win32Error.ERROR_ALREADY_EXISTS)
{
return NTStatus.STATUS_OBJECT_NAME_EXISTS;
}
else
{ {
state.LogToServer(Severity.Debug, "WriteFile: Cannot write '{0}'. Offset Out Of Range.", openFilePath);
return NTStatus.STATUS_DATA_ERROR; return NTStatus.STATUS_DATA_ERROR;
} }
catch (UnauthorizedAccessException) }
{ else if (exception is UnauthorizedAccessException)
state.LogToServer(Severity.Debug, "WriteFile: Cannot write '{0}'. Access Denied.", openFilePath); {
// The user may have tried to write to a readonly file return NTStatus.STATUS_ACCESS_DENIED;
return NTStatus.STATUS_ACCESS_DENIED; }
} else
{
return NTStatus.STATUS_DATA_ERROR;
} }
} }