void ProcessLocalGroupMember(List <SPUserInfo> users, string machineName, string domainAndName)
        {
            DomainAndName names = new DomainAndName(domainAndName);

            if (string.Compare(names.Domain, machineName, true, CultureInfo.InvariantCulture) == 0)
            {
                // Same machine so is a local group
                IntPtr bufPtr = IntPtr.Zero;
                try
                {
                    if (NetUserGetInfo(names.Domain, names.LoginName, 2, out bufPtr) == 0)
                    {
                        USER_INFO_2 user = new USER_INFO_2();
                        user = (USER_INFO_2)Marshal.PtrToStructure(bufPtr, typeof(USER_INFO_2));

                        SPUserInfo spUser = new SPUserInfo();
                        spUser.LoginName = domainAndName;
                        spUser.Name      = user.usri2_full_name;
                        users.Add(spUser);
                    }
                }
                finally
                {
                    if (bufPtr != IntPtr.Zero)
                    {
                        NetApiBufferFree(bufPtr);
                    }
                }
            }
            else
            {
                // Domain user
                try
                {
                    using (DirectoryEntry root = new DirectoryEntry("LDAP://" + names.Domain))
                    {
                        string filter = string.Format(CultureInfo.InvariantCulture, "(sAMAccountname={0})", names.LoginName);
                        using (DirectorySearcher searcher = new DirectorySearcher(root, filter, searchAttributes, SearchScope.Subtree))
                        {
                            ProcessSearchResults(searcher, users);
                        }
                    }
                }
                catch (COMException ex)
                {
                    throw new DomainGroupEnumerationException(String.Format(culture.Culture, culture.Resources.DomainGroupEnumFailed, "LDAP://" + names.Domain), ex);
                }
            }
        }
Beispiel #2
0
        public unsafe UserInfo2(USER_INFO_2 data)
        {
            string StringOrNull(char *c) => c == null ? null : new string(c);

            Name          = new string(data.usri2_name);
            FullName      = StringOrNull(data.usri2_full_name);
            Comment       = StringOrNull(data.usri2_comment);
            UserComment   = StringOrNull(data.usri2_usr_comment);
            UserPrivilege = data.usri2_priv;
            UserFlags     = data.usri2_flags;
            HomeDirectory = StringOrNull(data.usri2_home_dir);
            LogonScript   = StringOrNull(data.usri2_script_path);
            Parameters    = StringOrNull(data.usri2_parms);
            Workstations  = StringOrNull(data.usri2_workstations);
            LogonServer   = StringOrNull(data.usri2_logon_server);
        }
Beispiel #3
0
        private static UserProfileAuditor.USER_INFO_2 GetDomainUserInfo(string serverName, string userName)
        {
            USER_INFO_2 objUserInfo2 = new USER_INFO_2();

            try
            {
                IntPtr bufPtr; // because it's an OUT, we don't need to Alloc
                int    lngReturn = NetUserGetInfo(serverName, userName, 10, out bufPtr);
                if (lngReturn == 0)
                {
                    objUserInfo2 = (USER_INFO_2)Marshal.PtrToStructure(bufPtr, typeof(USER_INFO_2));
                }
                NetApiBufferFree(bufPtr);
                bufPtr = IntPtr.Zero;
            }
            catch (Exception)
            {
            }

            return(objUserInfo2);
        }
Beispiel #4
0
 public static extern NET_API_STATUS NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, ref USER_INFO_2 buf, int error);
Beispiel #5
0
 public unsafe UserInfo2(USER_INFO_2 data)
 {
 internal static extern UInt32  NetUserSetInfo(string servername, string username, UInt32 level, ref USER_INFO_2 userinfo, out UInt32 parm_err);
Beispiel #7
0
        public static List <User> StartAudit()
        {
            int EntriesRead;
            int TotalEntries;
            int Resume;

            IntPtr      bufPtr;
            List <User> lstUser = new List <User>();

            UserProfileAuditor.NetUserEnum(null, 2, 0,
                                           out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
            int           err         = Marshal.GetLastWin32Error();
            List <string> lstProfiles = RegistryUtil.GetUserProfiles();

            if (EntriesRead > 0)
            {
                UserProfileAuditor.USER_INFO_2[] Users = new UserProfileAuditor.USER_INFO_2[EntriesRead];
                IntPtr iter = bufPtr;
                for (int i = 0; i < EntriesRead; i++)
                {
                    Users[i] = (UserProfileAuditor.USER_INFO_2)Marshal.PtrToStructure(iter, typeof(UserProfileAuditor.USER_INFO_2));
                    iter     = (IntPtr)((int)iter + Marshal.SizeOf(typeof(UserProfileAuditor.USER_INFO_2)));

                    User user = new User();
                    user.UserName = Users[i].usri2_name;
                    string localGroup = string.Empty;
                    foreach (var item in GetLocalGroups(user.UserName))
                    {
                        localGroup += item + ";";
                    }
                    user.Groups           = localGroup.TrimEnd(new char[] { ';' });
                    user.FullName         = Users[i].usri2_full_name;
                    user.PasswordAge      = Users[i].usri2_password_age.ToString();
                    user.Description      = Users[i].usri2_comment;
                    user.LastLogin        = GetTimeFormElaspedSeconds((uint)Users[i].usri2_last_logon);
                    user.IsDisabled       = CheckFlagIsEnabled(Users[i].usri2_flags, UF_ACCOUNTDISABLE);
                    user.IsLocked         = CheckFlagIsEnabled(Users[i].usri2_flags, UF_LOCKOUT);
                    user.PasswordRequired = !CheckFlagIsEnabled(Users[i].usri2_flags, UF_PASSWD_NOTREQD);

                    GetSidDetails(user);
                    if (lstProfiles.Contains(user.SID))
                    {
                        lstProfiles.Remove(user.SID);
                    }
                    lstUser.Add(user);
                }
            }


            string serverName = GetDCName();

            foreach (string item in lstProfiles)
            {
                string userName = string.Empty;
                try
                {
                    userName = new SecurityIdentifier(item).Translate(typeof(NTAccount)).ToString();

                    User user = new User();

                    user.UserName = userName;
                    string localGroup = string.Empty;
                    foreach (var group in GetLocalGroups(user.UserName))
                    {
                        localGroup += group + ";";
                    }
                    user.Groups = localGroup.TrimEnd(new char[] { ';' });
                    string[] usersNme = user.UserName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                    GetSidDetails(user);
                    try
                    {
                        if (!string.IsNullOrEmpty(serverName))
                        {
                            USER_INFO_2 userInfo2 = GetDomainUserInfo(serverName, user.UserName);
                            user.FullName         = userInfo2.usri2_full_name;
                            user.FullName         = userInfo2.usri2_full_name;
                            user.PasswordAge      = userInfo2.usri2_password_age.ToString();
                            user.Description      = userInfo2.usri2_comment;
                            user.LastLogin        = GetTimeFormElaspedSeconds((uint)userInfo2.usri2_last_logon);
                            user.IsDisabled       = CheckFlagIsEnabled(userInfo2.usri2_flags, UF_ACCOUNTDISABLE);
                            user.IsLocked         = CheckFlagIsEnabled(userInfo2.usri2_flags, UF_LOCKOUT);
                            user.PasswordRequired = !CheckFlagIsEnabled(userInfo2.usri2_flags, UF_PASSWD_NOTREQD);
                        }
                    }
                    catch (Exception)
                    {
                    }

                    lstUser.Add(user);
                }
                catch (Exception)
                {
                }
            }

            UserProfileAuditor.NetApiBufferFree(bufPtr);
            return(lstUser);
        }