Example #1
0
        internal SmbTree GetSmbTree(string share, string service)
        {
            lock (this)
            {
                SmbTree t;
                if (share == null)
                {
                    share = "IPC$";
                }

                /*for (IEnumeration e = trees.GetEnumerator(); e.MoveNext(); )
                 * {
                 *      t = (SmbTree)e.Current;
                 *      if (t.Matches(share, service))
                 *      {
                 *              return t;
                 *      }
                 * }*/
                foreach (var e in Trees)
                {
                    t = (SmbTree)e;
                    if (t.Matches(share, service))
                    {
                        return(t);
                    }
                }

                t = new SmbTree(this, share, service);
                Trees.Add(t);
                return(t);
            }
        }
Example #2
0
 public override bool Equals(object obj)
 {
     if (obj is SmbTree)
     {
         SmbTree tree = (SmbTree)obj;
         return(Matches(tree.Share, tree.Service));
     }
     return(false);
 }
Example #3
0
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual DfsReferral GetDfsReferrals(NtlmPasswordAuthentication auth,
                                                     string path,
                                                     int rn)
        {
            SmbTree ipc = GetSmbSession(auth).GetSmbTree("IPC$", null);
            Trans2GetDfsReferralResponse resp = new Trans2GetDfsReferralResponse();

            ipc.Send(new Trans2GetDfsReferral(path), resp);
            if (resp.NumReferrals == 0)
            {
                return(null);
            }
            if (rn == 0 || resp.NumReferrals < rn)
            {
                rn = resp.NumReferrals;
            }
            DfsReferral dr = new DfsReferral();

            string[] arr        = new string[4];
            long     expiration = Runtime.CurrentTimeMillis() + Dfs.Ttl * 1000;
            int      di         = 0;

            for (;;)
            {
                dr.ResolveHashes = auth.HashesExternal;
                dr.Ttl           = resp.Referrals[di].Ttl;
                dr.Expiration    = expiration;
                if (path.Equals(string.Empty))
                {
                    dr.Server = Runtime.Substring(resp.Referrals[di].Path, 1).ToLower();
                }
                else
                {
                    DfsPathSplit(resp.Referrals[di].Node, arr);
                    dr.Server = arr[1];
                    dr.Share  = arr[2];
                    dr.Path   = arr[3];
                }
                dr.PathConsumed = resp.PathConsumed;
                di++;
                if (di == rn)
                {
                    break;
                }
                dr.Append(new DfsReferral());
                dr = dr.Next;
            }
            return(dr.Next);
        }
Example #4
0
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        public static void Logon(UniAddress dc, int port, NtlmPasswordAuthentication auth
                                 )
        {
            SmbTree tree = SmbTransport.GetSmbTransport(dc, port).GetSmbSession(auth).GetSmbTree
                               (LogonShare, null);

            if (LogonShare == null)
            {
                tree.TreeConnect(null, null);
            }
            else
            {
                Trans2FindFirst2         req  = new Trans2FindFirst2("\\", "*", SmbFile.AttrDirectory);
                Trans2FindFirst2Response resp = new Trans2FindFirst2Response();
                tree.Send(req, resp);
            }
        }
Example #5
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 #6
0
        /// <exception cref="System.UriFormatException"></exception>
        /// <exception cref="UnknownHostException"></exception>
        /*internal SmbFile(Jcifs.Smb.SmbFile context, string name, int type, int attributes
            , long createTime, long lastModified, long size)
            : this(context.IsWorkgroup0() ?
                new Uri(null, "smb://" + name + "/") : new Uri(context.url,
                name + ((attributes & ATTR_DIRECTORY) > 0 ? "/" : string.Empty)))*/
        internal SmbFile(SmbFile context, string name, int type, int attributes
            , long createTime, long lastModified, long size)
            : this(context.IsWorkgroup0() ?
                new Uri("smb://" + name + "/") : new Uri(context.Url.AbsoluteUri +
                name + ((attributes & AttrDirectory) > 0 ? "/" : string.Empty)))
        {
            Auth = context.Auth;
            if (context._share != null)
            {
                Tree = context.Tree;
                _dfsReferral = context._dfsReferral;
            }
            int last = name.Length - 1;
            if (name[last] == '/')
            {
                name = Runtime.Substring(name, 0, last);
            }
            if (context._share == null)
            {
                Unc = "\\";
            }
            else
            {
                if (context.Unc.Equals("\\"))
                {
                    Unc = '\\' + name;
                }
                else
                {
                    Unc = context.Unc + '\\' + name;
                }
            }

            if (!context.IsWorkgroup0())
            {
                Addresses = context.Addresses;
            }

            this._enableDfs = context.EnableDfs;

            this.Type = type;
            this._attributes = attributes;
            this._createTime = createTime;
            this._lastModified = lastModified;
            this._size = size;
            _isExists = true;
            _attrExpiration = _sizeExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
        }
Example #7
0
        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>context</code>. See the description above for examples of
        /// using the second <code>name</code> parameter. The <tt>shareAccess</tt>
        /// parameter controls what permissions other clients have when trying
        /// to access the same file while this instance is still open. This
        /// value is either <tt>FILE_NO_SHARE</tt> or any combination
        /// of <tt>FILE_SHARE_READ</tt>, <tt>FILE_SHARE_WRITE</tt>, and
        /// <tt>FILE_SHARE_DELETE</tt> logically OR'd together.
        /// </remarks>
        /// <param name="context">A base <code>SmbFile</code></param>
        /// <param name="name">A path string relative to the <code>context</code> file path</param>
        /// <param name="shareAccess">Specifies what access other clients have while this file is open.
        /// 	</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>context</code> and <code>name</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        /// <exception cref="UnknownHostException"></exception>
        public SmbFile(SmbFile context, string name, int shareAccess)
            : this(context.IsWorkgroup0() ? new Uri("smb://" + name) : new Uri(
                          context.Url.AbsoluteUri + name), context.Auth)
        {
            if ((shareAccess & ~(FileShareRead | FileShareWrite | FileShareDelete)) !=
                0)
            {
                throw new RuntimeException("Illegal shareAccess parameter");
            }

            if (!context.IsWorkgroup0())
            {
                this.Addresses = context.Addresses;

                if (context._share != null || context.Tree != null)
                {
                    Tree = context.Tree;
                    _dfsReferral = context._dfsReferral;
                }
            }

            this._shareAccess = shareAccess;
            this._enableDfs = context.EnableDfs;
        }
Example #8
0
        /// <summary>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory.
        /// </summary>
        /// <remarks>
        /// Constructs an SmbFile representing a resource on an SMB network such
        /// as a file or directory. The second parameter is a relative path from
        /// the <code>parent SmbFile</code>. See the description above for examples
        /// of using the second <code>name</code> parameter.
        /// </remarks>
        /// <param name="context">A base <code>SmbFile</code></param>
        /// <param name="name">A path string relative to the <code>parent</code> paremeter</param>
        /// <exception cref="System.UriFormatException">
        /// If the <code>parent</code> and <code>child</code> parameters
        /// do not follow the prescribed syntax
        /// </exception>
        /// <exception cref="UnknownHostException">If the server or workgroup of the <tt>context</tt> file cannot be determined
        /// 	</exception>
        public SmbFile(SmbFile context, string name)
            : this(context.IsWorkgroup0
                () ? new Uri("smb://" + name) : new Uri(context.Url.AbsoluteUri + name),
                context.Auth)
        {

            this._enableDfs = context.EnableDfs;

            if (!context.IsWorkgroup0())
            {
                Addresses = context.Addresses;

                if (context._share != null)
                {
                    Tree = context.Tree;
                    _dfsReferral = context._dfsReferral;
                }                
            }
        }
Example #9
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void DoConnect()
        {
            SmbTransport trans;
            UniAddress addr;
            addr = GetAddress();

            if (Tree != null && Tree.Session.transport.Address.Equals(addr))
            {
                trans = Tree.Session.transport;
            }
            else
            {
                trans = SmbTransport.GetSmbTransport(addr, Url.Port);
                Tree = trans.GetSmbSession(Auth).GetSmbTree(_share, null);
            }


            string hostName = GetServerWithDfs();
            if (_enableDfs)
            {
                Tree.InDomainDfs = Dfs.Resolve(hostName, Tree.Share, null, Auth) != null;
            }
            if (Tree.InDomainDfs)
            {
                Tree.ConnectionState = 2;
            }
            try
            {
                if (Log.Level >= 3)
                {
                    Log.WriteLine("doConnect: " + addr);
                }
                Tree.TreeConnect(null, null);
            }
            catch (SmbAuthException sae)
            {
                NtlmPasswordAuthentication a;
                SmbSession ssn;
                if (_share == null)
                {
                    // IPC$ - try "anonymous" credentials
                    ssn = trans.GetSmbSession(NtlmPasswordAuthentication.Null);
                    Tree = ssn.GetSmbTree(null, null);
                    Tree.TreeConnect(null, null);
                }
                else
                {
                    if ((a = NtlmAuthenticator.RequestNtlmPasswordAuthentication(Url.ToString(), sae)
                        ) != null)
                    {
                        Auth = a;
                        ssn = trans.GetSmbSession(Auth);
                        Tree = ssn.GetSmbTree(_share, null);
                        Tree.InDomainDfs = Dfs.Resolve(hostName, Tree.Share, null, Auth) != null;
                        if (Tree.InDomainDfs)
                        {
                            Tree.ConnectionState = 2;
                        }
                        Tree.TreeConnect(null, null);
                    }
                    else
                    {
                        if (Log.Level >= 1 && HasNextAddress())
                        {
                            Runtime.PrintStackTrace(sae, Log);
                        }
                        throw;
                    }
                }
            }
        }
Example #10
0
        internal SmbTree GetSmbTree(string share, string service)
        {
            lock (this)
            {
                SmbTree t;
                if (share == null)
                {
                    share = "IPC$";
                }
                /*for (IEnumeration e = trees.GetEnumerator(); e.MoveNext(); )
                {
                    t = (SmbTree)e.Current;
                    if (t.Matches(share, service))
                    {
                        return t;
                    }
                }*/
                foreach (var e in Trees)
                {
                    t = (SmbTree)e;
                    if (t.Matches(share, service))
                    {
                        return t;
                    }
                }

                t = new SmbTree(this, share, service);
                Trees.Add(t);
                return t;
            }
        }