Пример #1
0
        public List <string> GetLocalGroupMembers(string targetedComputer, string GroupName)
        {
            List <string> groupMembers = new List <string>();
            int           read;
            int           total;
            int           resume;
            IntPtr        pbuf;
            int           ret = NetLocalGroupGetMembers(targetedComputer, GroupName, 3, out pbuf, -1, out read, out total, out resume);

            if (ret != 0)
            {
                return(groupMembers);
            }
            List <string> members = new List <string>();

            if (read > 0)
            {
                var    m     = new LOCALGROUP_MEMBERS_INFO_3();
                IntPtr pItem = pbuf;
                for (int i = 0; i < read; ++i)
                {
                    Marshal.PtrToStructure(pItem, m);
                    pItem = new IntPtr(pItem.ToInt64() + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_3)));
                    groupMembers.Add(m.domainandname);
                }
            }
            NetApiBufferFree(pbuf);
            return(groupMembers);
        }
Пример #2
0
 public static unsafe extern int NetLocalGroupAddMembers(
     [In, MarshalAs(UnmanagedType.LPWStr)] string serverName,
     [In, MarshalAs(UnmanagedType.LPWStr)] string groupName,
     uint level,
     ref LOCALGROUP_MEMBERS_INFO_3 buf,
     uint totalentries
     );
Пример #3
0
        public static string[] GetLocalGroupMembers(string groupname)
        {
            int    read;
            int    total;
            int    resume;
            IntPtr pbuf;

            int ret = netapi.NetLocalGroupGetMembers(null, groupname, 3, out pbuf, -1, out read, out total, out resume);

            if (ret != 0)
            {
                throw new Win32Exception(ret);
            }

            List <string> members = new List <string>();

            if (read > 0)
            {
                var    m     = new LOCALGROUP_MEMBERS_INFO_3();
                IntPtr pItem = pbuf;
                for (int i = 0; i < read; ++i)
                {
                    Marshal.PtrToStructure(pItem, m);
                    pItem = new IntPtr(pItem.ToInt64() + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_3)));
                    members.Add(DomainUser.Parse(m.domainandname).ToString());
                }
            }
            netapi.NetApiBufferFree(pbuf);
            return(members.ToArray());
        }
Пример #4
0
        /// <summary>
        /// Function adds membership of one or more existing user accounts or global group accounts to an existing local group. The function does not change the membership status of users or global groups that are currently members of the local group.
        /// If the function succeeds, the return value is true. If the function fails, - throw new application exception
        /// </summary>
        /// <param name="serverName">string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. If this parameter is NULL, the local computer is used.</param>
        /// <param name="groupName">string that specifies the name of the local group to which the specified users or global groups will be added</param>
        /// <param name="userNames">string array that contains the data for the new local group members.</param>
        /// <returns>If the function succeeds, the return value is true. If the function fails, - throw new application exception</returns>
        /// <exception cref="ApplicationException">If the function fails, - throw new application exception</exception>
        public static bool AddUsersToLocalGroupByName(string serverName, string groupName, params string[] userNames)
        {
            try
            {
                if ((userNames == null) || (userNames.Length == 0))
                {
                    throw new ApplicationException(string.Format(Msg.CANNOT_ADD_NULL_USERS_TO_GROUP, groupName));
                }

                var users = new LOCALGROUP_MEMBERS_INFO_3[userNames.Length];
                for (int i = 0; i < userNames.Length; i++)
                {
                    users[i].Domain = userNames[i];
                }
                var result = NetLocalGroupAddMembers(serverName, groupName, 3, users, (uint)userNames.Length);
                if (result != NERR_Success)
                {
                    throw new Win32Exception(result);
                }
                return(true);
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format(Msg.CANNOT_ADD_USER_TO_LOCAL_GROUP, groupName), e);
            }
        }
        ICollection <SPUserInfo> EnumerateLocalGroup(SPUser group)
        {
            int    entriesRead;
            int    totalEntries;
            int    resume;
            IntPtr bufPtr;

            DomainAndName     names = new DomainAndName(group.LoginName);
            List <SPUserInfo> users = new List <SPUserInfo>();

            NetLocalGroupGetMembers(names.Domain, names.LoginName, 3, out bufPtr, -1, out entriesRead, out totalEntries, out resume);

            try
            {
                if (entriesRead > 0)
                {
                    LOCALGROUP_MEMBERS_INFO_3[] members = new LOCALGROUP_MEMBERS_INFO_3[entriesRead];
                    IntPtr pointer = bufPtr;
                    for (int i = 0; i < entriesRead; i++)
                    {
                        TimeSpan remaining = RemainingTime;
                        members[i] = (LOCALGROUP_MEMBERS_INFO_3)Marshal.PtrToStructure(pointer, typeof(LOCALGROUP_MEMBERS_INFO_3));
                        pointer    = (IntPtr)((int)pointer + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_3)));

                        ProcessLocalGroupMember(users, names.Domain, members[i].lgrmi3_domainandname);
                    }
                }
            }
            finally
            {
                NetApiBufferFree(bufPtr);
            }

            return(users);
        }
Пример #6
0
 internal static extern NET_API_STATUS NetLocalGroupGetMembers(
     string servername,
     string localgroupname,
     int level,
     ref LOCALGROUP_MEMBERS_INFO_3 bufptr,
     int prefmaxlen,
     ref int entriesread,
     ref int totalentries,
     ref IntPtr resumehandle);
Пример #7
0
        //把用户添加至本地用户组
        public static bool LocalGroupAddMembers(string GroupName, string UserName)
        {
            LOCALGROUP_MEMBERS_INFO_3 Members = new LOCALGROUP_MEMBERS_INFO_3();

            Members.DomainName = UserName.ToString();
            if (NetLocalGroupAddMembers(null, GroupName.ToString(), 3, ref Members, 1) != 0)
            {
                throw (new Exception("把用户添加至本地用户组时出现错误"));
            }
            else
            {
                return(true);
            }
        }
Пример #8
0
        NetDeleteGroupMember(
            string servername,
            string groupname,
            string username
            )
        {
            uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS;

            if (String.IsNullOrEmpty(servername))
            {
                servername = null;
            }
            if (String.IsNullOrEmpty(groupname))
            {
                groupname = null;
            }
            if (String.IsNullOrEmpty(username))
            {
                username = null;
            }

            LOCALGROUP_MEMBERS_INFO_3 lgmi_3 = new LOCALGROUP_MEMBERS_INFO_3();
            lgmi_3.lgrmi3_domainandname = username;

            IntPtr bufptr = IntPtr.Zero;
            bufptr = Marshal.AllocHGlobal(Marshal.SizeOf(lgmi_3));

            try
            {
                Marshal.StructureToPtr(lgmi_3, bufptr, false);

                if (!NetApiInitCalled)
                {
                    result = NetApiInit();
                    if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS)
                    {
                        return result;
                    }

                    NetApiInitCalled = true;
                }

                result = (uint)NetLocalGroupDelMembers(servername, groupname, 3, bufptr, 1);
            }
            catch (Exception)
            {
                result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE;
            }
            finally
            {
                if (bufptr != IntPtr.Zero)
                {
                    Marshal.DestroyStructure(bufptr, lgmi_3.GetType());
                    Marshal.FreeHGlobal(bufptr);
                }
            }

            return result;
        }
Пример #9
0
        NetGetGroupMembers(
            string servername,
            string groupname,
            out string [] members
            )
        {
            uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS;

            if (String.IsNullOrEmpty(servername))
            {
                servername = null;
            }
            if (String.IsNullOrEmpty(groupname))
            {
                groupname = null;
            }

            members = null;

            IntPtr bufPtr = IntPtr.Zero;
            IntPtr bufPtrStar = Marshal.AllocHGlobal(Marshal.SizeOf(bufPtr));

            IntPtr resumeHandle = IntPtr.Zero;
            IntPtr resumeHandleStar = Marshal.AllocHGlobal(Marshal.SizeOf(resumeHandle));

            try
            {
                int entriesRead = 0;
                int totalEntries = 0;

                Marshal.StructureToPtr(
                    resumeHandle,
                    resumeHandleStar,
                    false);

                Marshal.StructureToPtr(
                    bufPtr,
                    bufPtrStar,
                    false);

                if (!NetApiInitCalled)
                {
                    result = NetApiInit();
                    if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS)
                    {
                        return result;
                    }

                    NetApiInitCalled = true;
                }

                result = (uint)apiNetLocalGroupGetMembers(
                    servername,
                    groupname,
                    3,
                    out bufPtr,
                    MAX_PREFERRED_LENGTH,
                    out entriesRead,
                    out totalEntries,
                    resumeHandleStar);

                if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS)
                {
                    return result;
                }

                if (entriesRead > 0)
                {
                    members = new string[entriesRead];
                    LOCALGROUP_MEMBERS_INFO_3 memberStruct = new LOCALGROUP_MEMBERS_INFO_3();

                    IntPtr iter = bufPtr;

                    for (int i = 0; i < entriesRead; i++)
                    {
                        memberStruct = (LOCALGROUP_MEMBERS_INFO_3)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_MEMBERS_INFO_3));

                        //expected format for members[]: "domainName\userName"
                        if (!String.IsNullOrEmpty(memberStruct.lgrmi3_domainandname))
                        {
                            members[i] = memberStruct.lgrmi3_domainandname;
                        }
                        iter = (IntPtr)((long)iter + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_3)));
                    }
                }
            }
            catch (Exception)
            {
                result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE;
            }
            finally
            {
                //apiNetApiBufferFree(bufPtrStar);
                //apiNetApiBufferFree(resumeHandleStar);

            }

            //In the situation where a user account has been locally created then deleted on the target host (HOST-MACHINE)
            //then the API call NetLocalGroupGetMembers will return a result for each of these former users which is a part
            //of the specified group, but with only the domain, not the username.  The result: the 'members' array will contain
            //many elements of form 'HOST-MACHINE\' which are not relevant to LAC.
            if (members != null && members.Length > 0)
            {
                string[] scratch = new string[members.Length];
                int j = 0;

                for (int i = 0; i < members.Length; i++)
                {
                    string[] parts = members[i].Split(new char[] { '\\' });
                    if (parts != null && parts.Length == 2 && !String.IsNullOrEmpty(parts[1]))
                    {
                        scratch[j++] = members[i];
                    }
                }
                members = new string[j];
                for (int i = 0; i < j; i++)
                {
                    members[i] = scratch[i];
                }

            }
            return result;

        }
 internal extern static int NetLocalGroupAddMembers(string ServerName, string LocalGroupName,
                                                    uint Level, ref LOCALGROUP_MEMBERS_INFO_3 MemberInfo, uint TotalEntries);
Пример #11
0
 extern static int NetLocalGroupDelMembers([MarshalAs(UnmanagedType.LPWStr)] string sName, [MarshalAs(UnmanagedType.LPWStr)] string GroupName, int Level, ref LOCALGROUP_MEMBERS_INFO_3 bufPtr, int totalentries);
Пример #12
0
 public static extern NET_API_STATUS NetLocalGroupDelMembers([MarshalAs(UnmanagedType.LPWStr)] string psServer, [MarshalAs(UnmanagedType.LPWStr)] string psLocalGroup, int lLevel, ref LOCALGROUP_MEMBERS_INFO_3 uMember, int lMemberCount);
Пример #13
0
 public static extern NET_API_STATUS NetLocalGroupAddMembers([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string groupname, int level, ref LOCALGROUP_MEMBERS_INFO_3 buf, int totalentries);
 internal extern static int NetLocalGroupAddMembers(string ServerName, string LocalGroupName,
     uint Level, ref LOCALGROUP_MEMBERS_INFO_3 MemberInfo, uint TotalEntries);
Пример #15
0
 internal static extern UInt32  NetLocalGroupAddMembers(string servername, string groupname, UInt32 level, ref LOCALGROUP_MEMBERS_INFO_3 buf, UInt32 totalentries);