There are hundreds of error codes that may be returned by a CIFS server.
There are hundreds of error codes that may be returned by a CIFS server. Rather than represent each with it's own Exception class, this class represents all of them. For many of the popular error codes, constants and text messages like "The device is not ready" are provided.

The jCIFS client maps DOS error codes to NTSTATUS codes. This means that the user may recieve a different error from a legacy server than that of a newer varient such as Windows NT and above. If you should encounter such a case, please report it to jcifs at samba dot org and we will change the mapping.

Inheritance: System.IO.IOException
Example #1
0
        /// <exception cref="SharpCifs.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.");
            }
        }
Example #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);
        }
Example #3
0
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual void ResolveDfs(ServerMessageBlock request)
        {
            if (!_enableDfs)
            {
                Connect0();
                return;
            }

            if (request is SmbComClose)
            {
                return;
            }
            Connect0();
            DfsReferral dr = Dfs.Resolve(Tree.Session.transport.TconHostName, Tree.Share, Unc
                , Auth);
            if (dr != null)
            {
                string service = null;
                if (request != null)
                {
                    switch (request.Command)
                    {
                        case ServerMessageBlock.SmbComTransaction:
                        case ServerMessageBlock.SmbComTransaction2:
                            {
                                switch (((SmbComTransaction)request).SubCommand & 0xFF)
                                {
                                    case SmbComTransaction.Trans2GetDfsReferral:
                                        {
                                            break;
                                        }

                                    default:
                                        {
                                            service = "A:";
                                            break;
                                        }
                                }
                                break;
                            }

                        default:
                            {
                                service = "A:";
                                break;
                            }
                    }
                }
                DfsReferral start = dr;
                SmbException se = null;
                do
                {
                    try
                    {
                        if (Log.Level >= 2)
                        {
                            Log.WriteLine("DFS redirect: " + dr);
                        }
                        UniAddress addr = UniAddress.GetByName(dr.Server);
                        SmbTransport trans = SmbTransport.GetSmbTransport(addr, Url.Port);
                        trans.Connect();
                        Tree = trans.GetSmbSession(Auth).GetSmbTree(dr.Share, service);
                        if (dr != start && dr.Key != null)
                        {
                            dr.Map.Put(dr.Key, dr);
                        }
                        se = null;
                        break;
                    }
                    catch (IOException ioe)
                    {
                        if (ioe is SmbException)
                        {
                            se = (SmbException)ioe;
                        }
                        else
                        {
                            se = new SmbException(dr.Server, ioe);
                        }
                    }
                    dr = dr.Next;
                }
                while (dr != start);
                if (se != null)
                {
                    throw se;
                }
                if (Log.Level >= 3)
                {
                    Log.WriteLine(dr);
                }
                _dfsReferral = dr;
                if (dr.PathConsumed < 0)
                {
                    dr.PathConsumed = 0;
                }
                else
                {
                    if (dr.PathConsumed > Unc.Length)
                    {
                        dr.PathConsumed = Unc.Length;
                    }
                }
                string dunc = Runtime.Substring(Unc, dr.PathConsumed);
                if (dunc.Equals(string.Empty))
                {
                    dunc = "\\";
                }
                if (!dr.Path.Equals(string.Empty))
                {
                    dunc = "\\" + dr.Path + dunc;
                }
                Unc = dunc;
                if (request != null && request.Path != null && request.Path.EndsWith("\\") && dunc
                    .EndsWith("\\") == false)
                {
                    dunc += "\\";
                }
                if (request != null)
                {
                    request.Path = dunc;
                    request.Flags2 |= SmbConstants.Flags2ResolvePathsInDfs;
                }
            }
            else
            {
                if (Tree.InDomainDfs && !(request is NtTransQuerySecurityDesc) && !(request is SmbComClose
                    ) && !(request is SmbComFindClose2))
                {
                    throw new SmbException(NtStatus.NtStatusNotFound, false);
                }
                if (request != null)
                {
                    request.Flags2 &= ~SmbConstants.Flags2ResolvePathsInDfs;
                }
            }
        }
Example #4
0
 public override void Run()
 {
     lock (this)
     {
         try
         {
             for (; ; )
             {
                 Runtime.Notify(this);
                 Ready = true;
                 while (Ready)
                 {
                     Runtime.Wait(this);
                 }
                 if (N == -1)
                 {
                     return;
                 }
                 if (UseNtSmbs)
                 {
                     Reqx.SetParam(Dest.Fid, Off, N, B, 0, N);
                     Dest.Send(Reqx, Resp);
                 }
                 else
                 {
                     Req.SetParam(Dest.Fid, Off, N, B, 0, N);
                     Dest.Send(Req, Resp);
                 }
             }
         }
         catch (SmbException e)
         {
             this.E = e;
         }
         catch (Exception x)
         {
             E = new SmbException("WriterThread", x);
         }
         Runtime.Notify(this);
     }
 }
Example #5
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);
        }
		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;
		}
Example #7
0
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal void SessionSetup(ServerMessageBlock andx, ServerMessageBlock andxResponse
                                   )
        {
            lock (Transport())
            {
                INtlmContext                   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 sae)
                            {
                                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 = Config.getNtlmContextFactory().create(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 se)
                            {
                                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 sae)
                                {
                                    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 se)
                {
                    Logoff(true);
                    ConnectionState = 0;
                    throw;
                }
                finally
                {
                    Runtime.NotifyAll(transport);
                }
            }
        }