Пример #1
0
        public void InsertGCObject(GlobalCatalogMap obj)
        {
            if (nodb)
            {
                return;
            }
            var gc = db.GetCollection <GlobalCatalogMap>("globalcatalog");

            gc.Upsert(obj);
        }
Пример #2
0
        public bool GetGCMap(string username, out GlobalCatalogMap obj)
        {
            if (nodb)
            {
                obj = null;
                return(false);
            }
            var gc = db.GetCollection <GlobalCatalogMap>("globalcatalog");

            obj = gc.FindOne(x => x.Username.Equals(username));
            return(obj != null);
        }
Пример #3
0
        List <SessionInfo> GetNetSessions(string server, string ComputerDomain)
        {
            List <SessionInfo> toReturn = new List <SessionInfo>();
            IntPtr             PtrInfo  = IntPtr.Zero;
            int    val;
            IntPtr ResumeHandle = IntPtr.Zero;

            Type si10 = typeof(SESSION_INFO_10);

            val = NetSessionEnum(server, null, null, 10, out PtrInfo, -1, out int EntriesRead, out int TotalRead, ref ResumeHandle);

            SESSION_INFO_10[] results = new SESSION_INFO_10[EntriesRead];

            if (val == (int)NERR.NERR_Success)
            {
                IntPtr iter = PtrInfo;
                for (int i = 0; i < EntriesRead; i++)
                {
                    results[i] = (SESSION_INFO_10)Marshal.PtrToStructure(iter, si10);
                    iter       = (IntPtr)(iter.ToInt64() + Marshal.SizeOf(si10));
                }
            }

            NetApiBufferFree(PtrInfo);

            foreach (SESSION_INFO_10 x in results)
            {
                string username = x.sesi10_username;
                string cname    = x.sesi10_cname;

                if (cname != null && cname.StartsWith("\\", StringComparison.CurrentCulture))
                {
                    cname = cname.TrimStart('\\');
                }

                if (cname.Equals("[::1]"))
                {
                    cname = server;
                }

                if (cname.Equals("127.0.0.1"))
                {
                    cname = server;
                }

                if (username.EndsWith("$", StringComparison.CurrentCulture))
                {
                    continue;
                }

                if (username.Trim() == "" || username == "$" || username == CurrentUser)
                {
                    continue;
                }

                if (!ResolveCache.TryGetValue(cname, out string DNSHostName))
                {
                    try
                    {
                        DNSHostName = Dns.GetHostEntry(cname).HostName;
                        ResolveCache.TryAdd(cname, DNSHostName);
                    }
                    catch
                    {
                        DNSHostName = cname;
                    }
                }

                GlobalCatalogMap obj;
                if (options.SkipGCDeconfliction)
                {
                    obj = new GlobalCatalogMap
                    {
                        Username      = username,
                        PossibleNames = new List <string>()
                    };
                }
                else
                {
                    if (!manager.GetGCMap(username, out obj))
                    {
                        DirectorySearcher GCSearcher = helpers.GetDomainSearcher(ADSPath: GCPath);
                        GCSearcher.Filter = $"(&(samAccountType=805306368)(samaccountname={username}))";
                        GCSearcher.PropertiesToLoad.AddRange(new string[] { "distinguishedname" });
                        List <string> possible = new List <string>();
                        foreach (SearchResult r in GCSearcher.FindAll())
                        {
                            string dn     = r.GetProp("distinguishedname");
                            string domain = Helpers.DomainFromDN(dn);
                            possible.Add(domain.ToUpper());
                        }
                        GCSearcher.Dispose();
                        obj = new GlobalCatalogMap
                        {
                            PossibleNames = possible,
                            Username      = username
                        };
                        manager.InsertGCObject(obj);
                    }
                }

                if (DNSHostName == null)
                {
                    DNSHostName = cname;
                }

                if (obj.PossibleNames.Count == 0)
                {
                    //We didn't find the object in the GC at all. Default to computer domain
                    toReturn.Add(new SessionInfo
                    {
                        ComputerName = DNSHostName,
                        UserName     = $"{username}@{ComputerDomain}",
                        Weight       = 2
                    });
                }
                else if (obj.PossibleNames.Count == 1)
                {
                    //We found only one instance of the object
                    toReturn.Add(new SessionInfo
                    {
                        ComputerName = DNSHostName,
                        UserName     = $"{username}@{obj.PossibleNames.First()}",
                        Weight       = 1
                    });
                }
                else
                {
                    //Multiple possibilities. Add each one with a weight of 1 for the same domain as the computer
                    foreach (string p in obj.PossibleNames)
                    {
                        int weight;
                        if (p.ToUpper().Equals(ComputerDomain.ToUpper()))
                        {
                            weight = 1;
                        }
                        else
                        {
                            weight = 2;
                        }
                        toReturn.Add(new SessionInfo
                        {
                            ComputerName = DNSHostName,
                            UserName     = $"{username}@{p}",
                            Weight       = weight
                        });
                    }
                }
            }
            return(toReturn);
        }