示例#1
0
        /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
        internal virtual void CheckStatus(ServerMessageBlock req, ServerMessageBlock resp
                                          )
        {
            resp.ErrorCode = SmbException.GetStatusByCode(resp.ErrorCode);
            switch (resp.ErrorCode)
            {
            case NtStatus.NtStatusOk:
            {
                break;
            }

            case NtStatus.NtStatusAccessDenied:
            case NtStatus.NtStatusWrongPassword:
            case NtStatus.NtStatusLogonFailure:
            case NtStatus.NtStatusAccountRestriction:
            case NtStatus.NtStatusInvalidLogonHours:
            case NtStatus.NtStatusInvalidWorkstation:
            case NtStatus.NtStatusPasswordExpired:
            case NtStatus.NtStatusAccountDisabled:
            case NtStatus.NtStatusAccountLockedOut:
            case NtStatus.NtStatusTrustedDomainFailure:
            {
                throw new SmbAuthException(resp.ErrorCode);
            }

            case NtStatus.NtStatusPathNotCovered:
            {
                if (req.Auth == null)
                {
                    throw new SmbException(resp.ErrorCode, null);
                }
                DfsReferral dr = GetDfsReferrals(req.Auth, req.Path, 1);
                if (dr == null)
                {
                    throw new SmbException(resp.ErrorCode, null);
                }
                SmbFile.Dfs.Insert(req.Path, dr);
                throw dr;
            }

            case unchecked ((int)(0x80000005)):
            {
                break;
            }

            case NtStatus.NtStatusMoreProcessingRequired:
            {
                break;
            }

            default:
            {
                throw new SmbException(resp.ErrorCode, null);
            }
            }
            if (resp.VerifyFailed)
            {
                throw new SmbException("Signature verification failed.");
            }
        }
示例#2
0
        protected internal virtual IOException SeToIoe(SmbException se)
        {
            IOException ioe  = se;
            Exception   root = se.GetRootCause();

            if (root is TransportException)
            {
                ioe  = (TransportException)root;
                root = ((TransportException)ioe).GetRootCause();
            }
            if (root is Exception)
            {
                ioe = new IOException(root.Message);
                ioe.InitCause(root);
            }
            return(ioe);
        }
示例#3
0
        public override string ToString()
        {
            string c;

            switch (Command)
            {
            case SmbComNegotiate:
            {
                c = "SMB_COM_NEGOTIATE";
                break;
            }

            case SmbComSessionSetupAndx:
            {
                c = "SMB_COM_SESSION_SETUP_ANDX";
                break;
            }

            case SmbComTreeConnectAndx:
            {
                c = "SMB_COM_TREE_CONNECT_ANDX";
                break;
            }

            case SmbComQueryInformation:
            {
                c = "SMB_COM_QUERY_INFORMATION";
                break;
            }

            case SmbComCheckDirectory:
            {
                c = "SMB_COM_CHECK_DIRECTORY";
                break;
            }

            case SmbComTransaction:
            {
                c = "SMB_COM_TRANSACTION";
                break;
            }

            case SmbComTransaction2:
            {
                c = "SMB_COM_TRANSACTION2";
                break;
            }

            case SmbComTransactionSecondary:
            {
                c = "SMB_COM_TRANSACTION_SECONDARY";
                break;
            }

            case SmbComFindClose2:
            {
                c = "SMB_COM_FIND_CLOSE2";
                break;
            }

            case SmbComTreeDisconnect:
            {
                c = "SMB_COM_TREE_DISCONNECT";
                break;
            }

            case SmbComLogoffAndx:
            {
                c = "SMB_COM_LOGOFF_ANDX";
                break;
            }

            case SmbComEcho:
            {
                c = "SMB_COM_ECHO";
                break;
            }

            case SmbComMove:
            {
                c = "SMB_COM_MOVE";
                break;
            }

            case SmbComRename:
            {
                c = "SMB_COM_RENAME";
                break;
            }

            case SmbComDelete:
            {
                c = "SMB_COM_DELETE";
                break;
            }

            case SmbComDeleteDirectory:
            {
                c = "SMB_COM_DELETE_DIRECTORY";
                break;
            }

            case SmbComNtCreateAndx:
            {
                c = "SMB_COM_NT_CREATE_ANDX";
                break;
            }

            case SmbComOpenAndx:
            {
                c = "SMB_COM_OPEN_ANDX";
                break;
            }

            case SmbComReadAndx:
            {
                c = "SMB_COM_READ_ANDX";
                break;
            }

            case SmbComClose:
            {
                c = "SMB_COM_CLOSE";
                break;
            }

            case SmbComWriteAndx:
            {
                c = "SMB_COM_WRITE_ANDX";
                break;
            }

            case SmbComCreateDirectory:
            {
                c = "SMB_COM_CREATE_DIRECTORY";
                break;
            }

            case SmbComNtTransact:
            {
                c = "SMB_COM_NT_TRANSACT";
                break;
            }

            case SmbComNtTransactSecondary:
            {
                c = "SMB_COM_NT_TRANSACT_SECONDARY";
                break;
            }

            default:
            {
                c = "UNKNOWN";
                break;
            }
            }
            string str = ErrorCode == 0 ? "0" : SmbException.GetMessageByCode(ErrorCode);

            return("command=" + c + ",received=" + Received + ",errorCode=" + str
                   + ",flags=0x" + Hexdump.ToHexString(Flags & 0xFF, 4) + ",flags2=0x"
                   + Hexdump.ToHexString(Flags2, 4) + ",signSeq=" + SignSeq + ",tid=" + Tid + ",pid="
                   + Pid + ",uid=" + Uid + ",mid=" + Mid + ",wordCount=" + WordCount + ",byteCount="
                   + ByteCount);
        }
示例#4
0
        /// <exception cref="WinrtCifs.Smb.SmbException"></exception>
        internal void SessionSetup(ServerMessageBlock andx, ServerMessageBlock andxResponse
                                   )
        {
            lock (Transport())
            {
                NtlmContext                    nctx = null;
                SmbException                   ex   = null;
                SmbComSessionSetupAndX         request;
                SmbComSessionSetupAndXResponse response;
                byte[] token = new byte[0];
                int    state = 10;
                while (ConnectionState != 0)
                {
                    if (ConnectionState == 2 || ConnectionState == 3)
                    {
                        // connected or disconnecting
                        return;
                    }
                    try
                    {
                        Runtime.Wait(transport);
                    }
                    catch (Exception ie)
                    {
                        throw new SmbException(ie.Message, ie);
                    }
                }
                ConnectionState = 1;
                // trying ...
                try
                {
                    transport.Connect();
                    if (transport.Log.Level >= 4)
                    {
                        transport.Log.WriteLine("sessionSetup: accountName=" + Auth.Username + ",primaryDomain="
                                                + Auth.Domain);
                    }
                    Uid = 0;
                    do
                    {
                        switch (state)
                        {
                        case 10:
                        {
                            if (Auth != NtlmPasswordAuthentication.Anonymous && transport.HasCapability(SmbConstants
                                                                                                        .CapExtendedSecurity))
                            {
                                state = 20;
                                break;
                            }
                            request  = new SmbComSessionSetupAndX(this, andx, Auth);
                            response = new SmbComSessionSetupAndXResponse(andxResponse);
                            if (transport.IsSignatureSetupRequired(Auth))
                            {
                                if (Auth.HashesExternal && NtlmPasswordAuthentication.DefaultPassword != NtlmPasswordAuthentication
                                    .Blank)
                                {
                                    transport.GetSmbSession(NtlmPasswordAuthentication.Default).GetSmbTree(LogonShare
                                                                                                           , null).TreeConnect(null, null);
                                }
                                else
                                {
                                    byte[] signingKey = Auth.GetSigningKey(transport.Server.EncryptionKey);
                                    request.Digest = new SigningDigest(signingKey, false);
                                }
                            }
                            request.Auth = Auth;
                            try
                            {
                                transport.Send(request, response);
                            }
                            catch (SmbAuthException)
                            {
                                throw;
                            }
                            catch (SmbException se)
                            {
                                ex = se;
                            }
                            if (response.IsLoggedInAsGuest && Runtime.EqualsIgnoreCase("GUEST", Auth.
                                                                                       Username) == false && transport.Server.Security != SmbConstants.SecurityShare &&
                                Auth != NtlmPasswordAuthentication.Anonymous)
                            {
                                throw new SmbAuthException(NtStatus.NtStatusLogonFailure);
                            }
                            if (ex != null)
                            {
                                throw ex;
                            }
                            Uid = response.Uid;
                            if (request.Digest != null)
                            {
                                transport.Digest = request.Digest;
                            }
                            ConnectionState = 2;
                            state           = 0;
                            break;
                        }

                        case 20:
                        {
                            if (nctx == null)
                            {
                                bool doSigning = (transport.Flags2 & SmbConstants.Flags2SecuritySignatures
                                                  ) != 0;
                                nctx = new NtlmContext(Auth, doSigning);
                            }
                            if (SmbTransport.LogStatic.Level >= 4)
                            {
                                SmbTransport.LogStatic.WriteLine(nctx);
                            }
                            if (nctx.IsEstablished())
                            {
                                NetbiosName     = nctx.GetNetbiosName();
                                ConnectionState = 2;
                                state           = 0;
                                break;
                            }
                            try
                            {
                                token = nctx.InitSecContext(token, 0, token.Length);
                            }
                            catch (SmbException)
                            {
                                try
                                {
                                    transport.Disconnect(true);
                                }
                                catch (IOException)
                                {
                                }
                                Uid = 0;
                                throw;
                            }
                            if (token != null)
                            {
                                request  = new SmbComSessionSetupAndX(this, null, token);
                                response = new SmbComSessionSetupAndXResponse(null);
                                if (transport.IsSignatureSetupRequired(Auth))
                                {
                                    byte[] signingKey = nctx.GetSigningKey();
                                    if (signingKey != null)
                                    {
                                        request.Digest = new SigningDigest(signingKey, true);
                                    }
                                }
                                request.Uid = Uid;
                                Uid         = 0;
                                try
                                {
                                    transport.Send(request, response);
                                }
                                catch (SmbAuthException)
                                {
                                    throw;
                                }
                                catch (SmbException se)
                                {
                                    ex = se;
                                    try
                                    {
                                        transport.Disconnect(true);
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                                if (response.IsLoggedInAsGuest && Runtime.EqualsIgnoreCase("GUEST", Auth.
                                                                                           Username) == false)
                                {
                                    throw new SmbAuthException(NtStatus.NtStatusLogonFailure);
                                }
                                if (ex != null)
                                {
                                    throw ex;
                                }
                                Uid = response.Uid;
                                if (request.Digest != null)
                                {
                                    transport.Digest = request.Digest;
                                }
                                token = response.Blob;
                            }
                            break;
                        }

                        default:
                        {
                            throw new SmbException("Unexpected session setup state: " + state);
                        }
                        }
                    }while (state != 0);
                }
                catch (SmbException)
                {
                    Logoff(true);
                    ConnectionState = 0;
                    throw;
                }
                finally
                {
                    Runtime.NotifyAll(transport);
                }
            }
        }